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;
        }

1 Answer

1

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);
    }

Still not working with a rigidbody attached.

What isn't working?

It doesn't move at all when pressed.

I think that thiw will hwlp you: http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html Just for test, try setting a big velocity.

It works but it won't stop moving now.