- Home /
Moving right and Left
Can anyone tell me how to get a character to move to the right and left suddenly?
I used this before, but it doesn't work on Character Colliders(Makes them pass through the floor). The same thing happens when I use translate to move them, it doesn't take into account the collider.
//Moves right when z is pressed
if (Input.GetKey("z")){
transform.position -= Vector3.right * speed * 2* Time.deltaTime;
}
//Moves left when v is pressed
if (Input.GetKey("v")){
transform.position += Vector3.right * speed* 2 * Time.deltaTime;
}
Comment
Best Answer
Answer by IJM · Nov 07, 2010 at 11:49 AM
For something like that you can use Rigidbody: http://unity3d.com/support/documentation/Components/class-Rigidbody.html
And you set the Velocity if you want to move it in a direction:
if (Input.GetButtonDown ("z")) {
rigidbody.velocity = -(Vector3.right * speed * 2* Time.deltaTime);
}
I think that thiw will hwlp you: http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html
Just for test, try setting a big velocity.