第三人稱角色移動及自由移動視野(RigidBody實現)

來源:互聯網
上載者:User

標籤:同步   垂直   攝像機   arp   水平   start   unity   地形   位置   

重點:向量的運算.在獲得水平及垂直方向的速度之後,將方向進行重設,讓方向與視野同步(即:相機的方向與人物方向相同)

下面以一個執行個體來說明如何操作:

1.建立一個地形(Terrain),兩個正方體(Cube)(參照物),膠囊(Capsule)。把主攝像機放到Capsule下面當作子物體並且重設一下位置資訊.

(為了方便觀察可以建立幾個材質球給物體附上)

2.把攝像機往後來在Game視圖下可以達到效果即可:

3.建立兩個指令碼一個用來控制移動另外一個控制視野轉動:(我建立的move和freeLook兩個指令碼)

move指令碼內容:

using UnityEngine;using System.Collections;public class move : MonoBehaviour {public GameObject camer;//相機// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {Vector3 forward = camer.transform.TransformDirection (Vector3.forward);//記錄相機前方向Vector3 right=camer.transform.TransformDirection (Vector3.right);//記錄相機右方向float H = Input.GetAxis ("Horizontal");float V = Input.GetAxis ("Vertical");Vector3 he = H * right + V * forward;//將速度進行合成GetComponent<Rigidbody> ().MovePosition (transform.position+ he * 5 * Time.deltaTime);//控制移動}}

  freeLook指令碼內容:

using UnityEngine;using System.Collections;public class freeLook : MonoBehaviour {public GameObject camer;private float speed=5.0f;//轉速// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {camer.transform.RotateAround (this.transform.position,Vector3.up,speed*Input.GetAxis("Mouse X"));//相機以人物為中心,自身Y軸進行旋轉}}

4.把相機指定到指令碼:  

 

第三人稱角色移動及自由移動視野(RigidBody實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.