unity 鼠标控制第一人称视角及键盘控制移动

来源:互联网 发布:matlab 循环读取矩阵 编辑:程序博客网 时间:2024/05/17 07:33

脚本MouseLook(在主相机上):

using UnityEngine;using System.Collections;public class Mouselook : MonoBehaviour {    public enum RotationAxes{        MouseXAndY = 0,        MouseX =1,        MouseY =2    }    public RotationAxes axes = RotationAxes.MouseXAndY;    public float sensitivityHor = 9f;    public float sensitivityVert = 9f;    public float minmumVert = -45f;    public float maxmumVert = 45f;    private float _rotationX = 0;    // Use this for initialization    void Start () {}// Update is called once per framevoid Update () {        if (axes == RotationAxes.MouseX)        {            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);        }        else if (axes == RotationAxes.MouseY)        {            _rotationX = _rotationX - Input.GetAxis("Mouse Y") * sensitivityVert;            _rotationX = Mathf.Clamp(_rotationX, minmumVert, maxmumVert);                  float rotationY = transform.localEulerAngles.y;            transform.localEulerAngles = new Vector3(-_rotationX, rotationY, 0);        }         else        {            _rotationX-= Input.GetAxis("Mouse Y") * sensitivityVert;            _rotationX = Mathf.Clamp(_rotationX, minmumVert, maxmumVert);            float delta = Input.GetAxis("Mouse X") * sensitivityHor;            float rotationY = transform.localEulerAngles.y + delta;            transform.localEulerAngles = new Vector3(-_rotationX, rotationY, 0);        }}}

move脚本:
using UnityEngine;using System.Collections;//[RequireComponent(typeof(CharacterController))]//[AddComponentMenu("Control Script/move")]public class move : MonoBehaviour{    public CharacterController controller;    public Rigidbody rigidbody;    public float speed = 1;    // Use this for initialization    void Start()    {        rigidbody = this.GetComponent<Rigidbody>();        controller = this.GetComponent<CharacterController>();    }    //Move        // Update is called once per frame    void Update()    {        //Move        if (Input.GetKey("a"))            controller.SimpleMove(transform.right * -speed);        if (Input.GetKey("d"))            controller.SimpleMove(transform.right * speed);        if (Input.GetKey("w"))            controller.SimpleMove(transform.forward * speed);        if (Input.GetKey("s"))            controller.SimpleMove(transform.forward * -speed);    }}


原创粉丝点击