本篇文章給大家分享的是有關(guān)Unity中怎么實現(xiàn)人物旋轉(zhuǎn)和移動效果,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、泗洪ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的泗洪網(wǎng)站制作公司
具體內(nèi)容如下
旋轉(zhuǎn)
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 () { //水平旋轉(zhuǎn)就是以Y軸作為旋轉(zhuǎn)軸旋轉(zhuǎn),鼠標(biāo)移動量為偏移量 if (axes == RotationAxes.MouseX) { transform.Rotate (0, Input.GetAxis("Mouse X") * sensitivityHor, 0);//通過“增加”旋轉(zhuǎn)角度進(jìn)行旋轉(zhuǎn)(X,Y,Z為對應(yīng)方向的增加量),一般用于無限制旋轉(zhuǎn) } 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);//直接“設(shè)置”旋轉(zhuǎn)角度進(jìn)行旋轉(zhuǎn),一般用于有限制旋轉(zhuǎn) } 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); } }}
移動
using System.Collections;using System.Collections.Generic;using UnityEngine; [RequireComponent(typeof(CharacterController))]//如果對象沒有該組件,則創(chuàng)建一個[AddComponentMenu("Control Script/FPS Input")]//可以在Add Component查到 public class FPSInput : MonoBehaviour { private CharacterController _characterController; public float speed = 10.0f; public float gravity = -9.8f; // Use this for initialization void Start () { _characterController = GetComponent();//獲取對象里的某一組件 } // Update is called once per frame void Update () { float deltaX = Input.GetAxis ("Horizontal") * speed; float deltaZ = Input.GetAxis ("Vertical") * speed; Vector3 movement = new Vector3 (deltaX, 0, deltaZ); movement = Vector3.ClampMagnitude (movement, speed);//保持各方向的速度相同 movement.y = gravity; movement = movement * Time.deltaTime;//向量可以直接乘以一個數(shù),Time.deltaTime確保在每臺計算機上的速度相同 movement = transform.TransformDirection (movement);//將本地坐標(biāo)轉(zhuǎn)換為世界坐標(biāo) _characterController.Move(movement);//通過CharacterController進(jìn)行移動而不是transform.Translate }}
以上就是Unity中怎么實現(xiàn)人物旋轉(zhuǎn)和移動效果,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:Unity中怎么實現(xiàn)人物旋轉(zhuǎn)和移動效果
網(wǎng)站URL:
http://weahome.cn/article/ggdhgd.html