- Home /
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,
Answer by _MGB_ · Feb 04, 2015 at 11:29 AM
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;
Hi,
Well, it's work, as long as we don't want to go back... If we press back button, we begin to fly... Very, very, very far away. It works when we go forward, but with a huge slowdown, it isn't the same made than my previous script. When I see at the vector of my ball (in the inspector), I see that there is forces at Y applied, approximatly 5 when we go back, and -5 we we go front.
It is the script, tell me if I did a mistake :)
var speed = 8.0;
var force : Vector3;
var vec : Vector3;
function Update () {
if (Input) {
vec.x = Input.GetAxis("Horizontal") * 25 * Time.deltaTime;
vec.z = Input.GetAxis("Vertical") * 25 * Time.deltaTime;
vec = Camera.main.transform.TransformDirection(vec.x, 0, vec.z);
force = vec.normalized * speed;
rigidbody.AddForce(force);
}
}
It is in JavaScript, I see that you gave me a C# script, is it better ?
Sorry for the time I take to answer I wasn't available ^^
Thanks for your answers you're a great help.
EDIT : I think I understand why there is a slowdown, it is because of the force on Y. And so the made will be the same as my previous.
EDIT 2 : A short screen : http://puu.sh/fvOlZ/4bb42782a9.jpg (Don't look at the map, it isn't ended)
Sorry I didn't copy down well.. It works, THAN$$anonymous$$S !
Answer by AngryBurritoCoder · Feb 04, 2015 at 11:18 AM
I can't help by coding atm ( on a train on phone ) , but there is a few solutions
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
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)
$$anonymous$$GB wrote the code :) check his out
Your answer
Follow this Question
Related Questions
RTS Camera movement wrong after rotation 1 Answer
Switch between Camera's 2 Answers
character view range 2 Answers
Camera Scripting Help (Sims Style) 0 Answers
Camera rotation and position problem,Camera rotation and camera position 0 Answers