本文實例為大家分享了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<Rigidbody> (); 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實現人物旋轉和移動效果-創新互聯
鏈接分享:http://newbst.com/article26/dgehjg.html
成都網站建設公司_創新互聯,為您提供關鍵詞優化、網站設計公司、微信公眾號、品牌網站制作、App開發、網站改版
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯