Moving where the mouse looks.

Hello,

I have a marble in my game. I would like to controll in with my mouse, so when I press W, the marbe will go to the direction where the camera is looking.

How should I do it?

Thanks!

You need to get the 3D position of the mouse when the player clic, for that, you cast a ray from the mouse through the screen like that :

var hit : RaycastHit;  
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (Physics.Raycast (ray, hit)) 
{
    nextPosition = hit.point;
}

Then, you need to move your marble toward "nextPosition", there is many ways to do that. It's probably a sphere collider with a rigidbody right ? So you can use

  • rigidbody.AddForce(...), wich will be realistic but not accurate,
  • or transform.Translate(...) and transform.Rotate(...), more complicated, but the marble will reach exactly the point you clicked.