U3D控制摄像头第一人称WASD移动旋转

//控制人物wasd移动,加到刚性物体或者摄像头

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class MOVE : MonoBehaviour {

    int MoveSpeed = 16;

 

 

    // Use this for initialization

    void Start () {

      

    }

   

    // Update is called once per frame

    void Update () {

        if (Input.GetKey(KeyCode.W))

        {

 

            this.transform.Translate(Vector3.forward * Time.deltaTime * MoveSpeed);

 

        }

        if (Input.GetKey(KeyCode.S))

        {

 

            this.transform.Translate(Vector3.back * Time.deltaTime * MoveSpeed);

 

        }

        if (Input.GetKey(KeyCode.A))

        {

 

            this.transform.Translate(Vector3.left * Time.deltaTime * MoveSpeed);

 

        }

        if (Input.GetKey(KeyCode.D))

        {

 

            this.transform.Translate(Vector3.right * Time.deltaTime * MoveSpeed);

 

        }

  

 

     

  

    }

}

 

//控制摄像头旋转加到摄像头上

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class xuanzhuan : MonoBehaviour {

 

     public float moveSpeed;

 

    void Start()

    {

        moveSpeed = 60f; //移动速度

    }

 

 

    void Update()

    {

        Vector3 angle = Vector3.zero;

 

        angle.x = transform.localEulerAngles.x - Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;

        angle.y = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;

 

        transform.localEulerAngles = angle;

 

     

 

    }

}

上一篇:Unity*移动拖动物品


下一篇:Unity 之游戏物体的移动篇