Unity中FPS类小游戏的简单制作

1.摄像机放Player上,射线机代码

using UnityEngine;
using System.Collections;

public class Camera : MonoBehaviour {

    public GameObject player;
    private Vector3 rot = new Vector3(0, 0, 0);
    public float speed;
    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {
        Move111();
    }

    void Move111() {
        float MouseX = Input.GetAxis("Mouse X") * speed;
        float MouseY = Input.GetAxis("Mouse Y") * speed;

        rot.x = rot.x - MouseY;
        rot.y = rot.y + MouseX;
        rot.z = 0;
        transform.eulerAngles = rot;
        player.transform.eulerAngles = new Vector3(0, rot.y, 0);

        if (rot.x <= -70) {
            transform.eulerAngles = new Vector3(-70, rot.y, 0);
        }
        if (rot.x >= 10) {
            transform.eulerAngles = new Vector3(10, rot.y, 0);
        }
    }
}

2.僵尸身上的代码

using UnityEngine;
using System.Collections;

public class XunLu : MonoBehaviour {
    GameObject tar;
    public float speed;
    float dis;
    Animator anim;
    //NavMeshAgent mynav;
    // Use this for initialization
    void Start() {
        anim = GetComponent();
        tar = GameObject.Find("Ppp");
    }

    // Update is called once per frame
    void Update() {
        /* mynav.*/    //此处不可以,寻找的是mynav
        GetComponent().SetDestination(tar.transform.position);
        GetComponent().speed = speed;
        Donghua();
    }


    void Donghua() {
        dis = Vector3.Distance(transform.position, tar.transform.position);

        if (dis <= 1.5f + 0.5f) {
            anim.SetBool("New Bool", true);
        } else {
            anim.SetBool("New Bool", false);
        }

    }
}
3.Player身上的配合角色控制器的代码

using UnityEngine;
using System.Collections;

public class Ppp : MonoBehaviour {
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;
    void Update() {
        CharacterController controller = GetComponent();
        if (controller.isGrounded) {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;

        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
}
4.游戏控制器代码

using UnityEngine;
using System.Collections;

public class GameCotroller1 : MonoBehaviour {
    public GameObject[] ZuoBiao;
    public GameObject Enemy;

    public GameObject Q1;
    public GameObject Q2;
    int i = 0;
    // Use this for initialization
    void Start() {
        StartCoroutine("Spawn");
    }

    // Update is called once per frame
    void Update() {
        Shexian();
        HuanQ();
    }

    IEnumerator Spawn() {
        while (true) {
            int i = Random.Range(0, 4);
            Instantiate(Enemy, ZuoBiao[i].transform.position, ZuoBiao[i].transform.rotation);
            yield return new WaitForSeconds(Random.Range(2.0f, 3.0f));

        }
    }
    public void Shexian() {
        RaycastHit hit;
        Vector3 rayPosition = new Vector3(Camera.main.pixelWidth * 0.5f, Screen.height * 0.5f);
        Ray ray = Camera.main.ScreenPointToRay(rayPosition);   //定义准备一条射线
        if (Input.GetMouseButtonDown(0)) {
            Physics.Raycast(ray, out hit);
            if (hit.transform.tag == "Enemy") {
                hit.transform.gameObject.GetComponent().SetTrigger("New Trigger");
                hit.transform.gameObject.GetComponent().Stop();
                Destroy(hit.transform.gameObject, 3.0f);
                hit.transform.gameObject.GetComponent().enabled = false;

            }
        }
    }

    void HuanQ() {
        if (Input.GetKeyDown("q")) {
            i = i + 1;//1 2
            if (i % 2 != 0) {
                Q1.SetActive(false);
                Q2.SetActive(true);
                print("1111");
            }
            if (i % 2 == 0) {
                print("2222");
                Q2.SetActive(false);
                Q1.SetActive(true);
            }
        }
    }
}

代码下载:

https://pan.lanzou.com/i0fzskd




你可能感兴趣的:(自学)