using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouselook : MonoBehaviour {
//MouseLook.cs
//无重力:放在人上MouseXAndY
//加重力: 人水平MouseX 摄像机垂直MouseY 原因:P39
public enum RotationAxes
{
MouseXAndY=0,
MouseX=1,
MouseY=2
}
public RotationAxes axes = RotationAxes.MouseXAndY;
public float speedV = 9.0f;
public float speedH = 9.0f;
public float maxhead = 45.0f;
public float minhead = -45.0f;
private float _rotationX = 0.0f;
// Use this for initialization
void Start () {
Rigidbody body = GetComponent<Rigidbody>();
if (body != null) body.freezeRotation = true;//禁止旋转
}
// Update is called once per frame
void Update () {
if (axes == RotationAxes.MouseX) { transform.Rotate(0, Input.GetAxis("Mouse X")*speedH, 0); }
if (axes == RotationAxes.MouseY) {
_rotationX -= Input.GetAxis("Mouse Y")*speedV;
_rotationX = Mathf.Clamp(_rotationX, minhead, maxhead);//限仰视俯视角度范围
float _rotationY = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(_rotationX, _rotationY, 0);//设置旋转角度
}
if (axes == RotationAxes.MouseXAndY) {
_rotationX -= Input.GetAxis("Mouse Y")*speedV;//注意是-=
_rotationX = Mathf.Clamp(_rotationX, minhead, maxhead);//水平Verital
float _rotationY = transform.localEulerAngles.y+ Input.GetAxis("Mouse X") * speedH;
transform.localEulerAngles=new Vector3( _rotationX, _rotationY,0);
}
}
}
本文介绍了一个Unity中的鼠标视角控制脚本MouseLook.cs。该脚本实现了三种不同的旋转轴配置:仅鼠标X轴、仅鼠标Y轴及同时使用XY轴。通过调整参数可以控制视角旋转速度与限制头部仰俯角度。
2万+

被折叠的 条评论
为什么被折叠?



