您的位置:首页 > 移动开发 > Unity3D

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

2017-10-21 15:40 946 查看
脚本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 frame
void 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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity