- Home /
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?
Answer by Joshua · Jul 30, 2011 at 01:31 PM
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.
Your answer
Follow this Question
Related Questions
I want to make a train go round a track using physics? 7 Answers
Bend object with physics interaction 0 Answers
Predicting land position with drag applied 0 Answers
What is the best way to apply force, then come to a smooth stop on control release? 1 Answer
Calculating required force for pushing a body to a desired position at once 0 Answers