Wednesday, September 17, 2014

The summer of realizing object movement in Unity3D

1. Using Translate() function:

transform.Translate(
 speed*Input.GetAxis("Horizontal")*Time.deltaTime,
 speed*Input.GetAxis("Vertical")*Time.deltaTime,
0);

"speed" is the variable to controller the move speed.

Input.GetAxis("Horizontal") gets the states of left and right button: if left button is pressed return -1 and if right button is pressed return 1;
Input.GetAxis("Vertical") gets the states of up and down button: up returns 1 and down returns -1.

Time.deltalTime is constant which stands for the time interval between frames. Its value is about 0.02 seconds. Here it is just used as a constant to reduce the move speed.

2. Using Vector to replace x,y,z:

transform.Translate(
  Vector3.right*speed*Input.GetAxis("Horizontal")*Time.deltaTime
)

Vector3.right is a constant vector equals (1,0,0);
The similar as Vector3.up, Vector3.down, Vector3.right

3. Using velocity attribute:

velocity is a Vector3 type attribute of CharacterController and RigidBody which describes the direction and speed of the movement.
For example:
var bomb:Rigidbody;
bomb.velocity=new Vector3(3*speed,0,0)

4. CharacterController.Move:

CharacterController.Move

Attempts to move the controller by motion, the motion will only be constrained by collisions. It will slide along colliders.CollisionFlags is the summary of collisions that occurred during the Move. This function does not apply any gravity.


No comments:

Post a Comment