以下以3D專案為例:
一、
建立Rigidbody Component組件,
於建立一C#程式,於Update方法中使用Input.GetAxis方法,並建立一Vector3區域變數:
void Update() { Vector3 moveInput = new Vector3(Input.GetAxis("Horizontal"),0f,Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * moveSpeed; }
Vector3.normalized簡而言之便是將一Vector3向量歸於1,以便另立變數予以加乘。
接著在FixedUpdate方法中使用Rigidbody.MovePosition:
public void FixedUpdate() { myRigidbody.MovePosition(transform.position + velocity * Time.fixedDeltaTime); }
二、
建立Rigidbody Component組件,
並在FixedUpdate方法中使用Rigidbody.AddForce方法:
void FixedUpdate() { rb.AddForce(transform.forward * thrust); }
三、
在Update方法中使用Transform.translate方法:
void Update() { transform.Translate(Vector3.forward * Time.deltaTime); transform.Translate(Vector3.up * Time.deltaTime, Space.World); }
以上有錯敬請不吝指正。
沒有留言:
張貼留言