Unity实现人物旋转和移动效果-创新互联
本文实例为大家分享了Unity实现人物旋转和移动的具体代码,供大家参考,具体内容如下

旋转
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour {
public enum RotationAxes
{
MouseXandY = 0,
MouseX = 1,
MouseY = 2
}
public RotationAxes axes = RotationAxes.MouseXandY;
public float sensitivityHor = 9.0f;
public float sensitivityVert = 9.0f;
public float minVert = -45.0f;
public float maxVert = 45.0f;
private float _rotationX = 0;
// Use this for initialization
void Start () {
Rigidbody body = GetComponent ();
if (body != null) {
body.freezeRotation = true;
}
}
// Update is called once per frame
void Update () {
//水平旋转就是以Y轴作为旋转轴旋转,鼠标移动量为偏移量
if (axes == RotationAxes.MouseX) {
transform.Rotate (0, Input.GetAxis("Mouse X") * sensitivityHor, 0);//通过“增加”旋转角度进行旋转(X,Y,Z为对应方向的增加量),一般用于无限制旋转
} else if (axes == RotationAxes.MouseY) {
_rotationX -= Input.GetAxis ("Mouse Y") * sensitivityVert;
_rotationX = Mathf.Clamp (_rotationX, minVert, maxVert);//限制_rotationX的值在minVert与minVert之间
float rotationY = transform.localEulerAngles.y;
//Debug.Log ("rotationX:"+_rotationX+","+Input.GetAxis ("Mouse Y") * sensitivityVert);
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);//直接“设置”旋转角度进行旋转,一般用于有限制旋转
} else {
_rotationX -= Input.GetAxis ("Mouse Y") * sensitivityVert;
_rotationX = Mathf.Clamp (_rotationX, minVert, maxVert);
float delta = Input.GetAxis ("Mouse X") * sensitivityHor;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
} 另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
标题名称:Unity实现人物旋转和移动效果-创新互联
URL网址:http://www.jxjierui.cn/article/dgehjg.html


咨询
建站咨询
