Adding force to forward direction

Hello,

I am creating a game, where the player controlls a marble. The marble rolls to the direction, where the camera looks. I would like to create an extra speed pickup.

I want the extra force to be applied to the direction where the camera looks, but if I use rigidbody.AddForce (Camera.main.transform.forward * 1000); then for example the force is applied to up, if I look up.

Also, I can’t use this.transform.forward, because the marble is rolling. I would like to archive it to be a power up like the extra speed in Marble Blast.

How should I do it?

You probably want it in the direction from the camera to the marble on the x,z plane - ignoring height so it’s forward and not downwards.

var directionVector : Vector3 = transform.position - Camera.main.transform.position;
directionVector.y = 0;
rigidbody.AddForce( Vector3.Normalize( directionVector ) * force );

How do you get this directionVector? Your.position-camera.position gets you a vector pointing from the camera to you. We remove the height, so the force will be only forward and we normalize the vector because the vector doesn’t only point from the camera to you, it’s also that long - and since we want a magnitude of one we normalize.