- Home /
Problem when applying forces to a ball
Hi,
I've got a problem with my game, I want to make forces on a ball to rotate it and to move it forward. I've asked before for a problem with my camera and mouse (http://answers.unity3d.com/questions/892634/how-to-apply-a-force-forward-depending-of-the-came.html), and now, I've got a new problem. And the people who responded me don't tell me more (they're maybe dead ? :D, no, they answer other ^^)
Well so the problem is that I want to make forces in terms of the view of the camera, it work... a few... I don't want to apply force in the Y axe (the ball don't fly), but forces is applyed in the Y axe and I don't understand why..
When we go forward, there is -5 applyed on the Y-axe which slowdown hardly the ball and when we go back... OMG 5 in Y.. We begin to soar (it is the best word I think but fly is more understandable).
Well this is the code (in Javascript), you can give me C# script, I will understand :
// Initialisation
var speed = 8.0;
var force : Vector3;
var vec : Vector3;
function Update () {
if (Input) { // If we push any button
vec.x = Input.GetAxis("Horizontal") * 25 * Time.deltaTime; // X-axe
vec.z = Input.GetAxis("Vertical") * 25 * Time.deltaTime; // Z-axe
vec = Camera.main.transform.TransformDirection(vec.x, 0, vec.z);
// As you see, 0 to Y-axe, and it is in terms of the camera's view
force = vec.normalized * speed;
// Normalize the vector and multiply with the speed
rigidbody.AddForce(force); // Apply the Vector3 to the rigidbody
}
}
Well, I don't understand the prob.. A short screen : http://puu.sh/fvOlZ/4bb42782a9.jpg as you see, there is force applyed on the Y axe.
Thank you a lot,
IceBlack,
not sure but... ins$$anonymous$$d of
vec.x = Input.GetAxis("Horizontal") * 25 * Time.deltaTime; // X-axe
vec.z = Input.GetAxis("Vertical") * 25 * Time.deltaTime; // Z-axe
vec = Camera.main.transform.TransformDirection(vec.x, 0, vec.z);
try...
vec.x = Input.GetAxis("Horizontal") * 25 * Time.deltaTime; // X-axe
vec = Camera.main.transform.TransformDirection(vec.x, 0, 0);
vec.z = Input.GetAxis("Vertical") * 25 * Time.deltaTime; // Z-axe
Hi, firstly sorry for the time a make to answer.
I'm sorry but it doesn't work. I can't translate now on the Z axis and I need ^^
I try a thing. I make a EmptyObject at the position of my ball and I put it on my camera. Also I change my code.
var origine : GameObject;
[.....]
vec = origine.transform.TransformDirection(vec.x, 0, vec.z);
And now the force on Y is less. I think I understand thanks to this the prob. The Empty Object is bit high from my ball so the ball want to go into. I think.
EDIT : no sorry it doesn't work, so I don't understand the prob
Answer by IceBlackSanctum · Feb 10, 2015 at 08:31 PM
Oh god... I found the problem....
I put "vec.y = 0;" after "vec = origine.transform.TransformDirection(vec.x, 0, vec.z);" and it work great. I don't understand why.. But it work as that. So the code is :
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);
vec.y = 0;
force = vec.normalized * speed;
rigidbody.AddForce(force);
}
}
Thanks a lot !!