How to apply a force forward depending of the camera

Hi,

Sorry I have not properly explained the problem.

I’m making a game with a ball. It is handled with the arrows of the keyboard and the mouse for the view around the ball.
But there is a problem. I succeeded to do the movement of the ball and of the camera but no way to linked them.

I would like that when I turn the camera the “forward” of the ball turn itself. I so always go forward if I press the forward arrow (depending on the view of the camera), and if I push the back button I go back.

The mouvement is made thanks to forces (I have to keep the forces I need for the game).

Here the script for the mouvement of the ball :

var vitesseDeplacement = 8.0;
function Update () {
	if (Input) {
		rigidbody.AddForce(Input.GetAxis("Horizontal") * 25 * vitesseDeplacement * Time.deltaTime,0,Input.GetAxis("Vertical") * 25 * vitesseDeplacement * Time.deltaTime);
	}
}

And here for the camera :

var sensibilite : int = 3;

function Update () {
	transform.Rotate(0, Input.GetAxis ("Mouse X") * sensibilite, 0);
}

I surely checked the tutorial of the “Roll-a-Ball”, look at the script of character in Unity but I lose the forces with it. And check at Unity Manual and I didn’t found the correct command…

I hope you will help me,

Thanks for your answer,

IceBlackSanctum,

I can’t help by coding atm ( on a train on phone ) , but there is a few solutions

  1. You can apply the force to the ball based on camera position, so basically apply the force to the ball in the direction the camera faces, (ignoring the y axis so the ball doesn’t start to fly xd ) , with this method the ball won’t rotate itself to the direction of the camera though

  2. When input is being taken and camera is moved the ball rotated to always face forward in the direction of the camera. So basically you allign the ball with camera rotation, again ignoring all axis except y ( so your ball doesn’t fly off again)

You can transform the force vector by the camera using something like:

vec.x = Input.GetAxis("Horizontal");
vec.z = Input.GetAxis("Vertical");
vec = Camera.main.transform.TransformDirection(vec);  // Make relative to main camera
vec.y = 0;  // optional for no y movement.
Vector3 force = vec.normalized * Magnitude;