method One : Change the transform of the object
Public class Exampleclass:monobehaviour { publicfloat5; // the move to follow the camera is written in Lateupdate. void lateupdate () { transform. Translate (00, Time.deltatime * speed );} }
method Two : Use the displacement function of the rigid body moveposition ()
Public classExampleclass:monobehaviour { Public floatSpeed =6.0f; Vector3 movement; voidStart () {playerrigidbody=Rigidbody; } voidfixedupdate () {//Role Control floatH = Input.getaxisraw ("Horizontal");//Get Horizontal values floatv = Input.getaxisraw ("Vertical");//get the vertical axis valueMove (H, v); } voidMove (floatHfloatv) {movement. Set (H,0, V); Movement= movement.normalized * Speed *Time.deltatime; Playerrigidbody.moveposition (Transform.position+movement); }}
mode three : Change the speed of the rigid body rigidbody.velocity. Velocity is a vector and has directionality.
public class Exampleclass:monobehaviour { // Get the joystick value float movehorizontal = Input.getaxis ( " horizontal " float movevertical = Input.getaxis ( vertical " ); // change speed rigidbody.velocity = new Vector3 (movehorizontal, 0.0f , movevertical) * speed;}
How to move the "Unity note" role