How can I move sphere with WASD keys correctly?

Hi, I have been trying to move this sphere (package included) with WASD keys, now it is moving the sphere with mouse but I prefer keyboard. It moves the sphere acording to global coordinates, how can i make it move so that when i press for example left, it moves left not global left?


This won't tell you how to do it with script but it is a very good series of tutorials that will help you set up a WASD player in seconds.

Use Transform.TransformDirection() to transform your direction vector into the local space of your sphere before you apply it.

Note that if your sphere is rolling, it may be simplest for you if you have a parent game object to it that does not roll.

Can you post the code? It makes it much easier to answer your question.

I don't really want to walk through a gigantic package right now.

Here are a few things that may help

  • `rigidbody.AddRelativeForce()` - This adds force in a local direction, so using the Vector3(0, 0, 10) would move it along the relative Z axis, not the global one

  • `transform.forward` - Returns the relative forward vector, I find it useful when used like this: `transform.Translate(transform.forward * speed)`

This will make your sphere move:

var speed = 7.0; var rotateSpeed = 3.0;

function Update () {

var controller : CharacterController = GetComponent(CharacterController); transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0); var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed * Input.GetAxis ("Vertical"); controller.SimpleMove(forward * curSpeed);



Attatch this to the sphere that you want to move.

Hope that helped :)