- Home /
Applying Constant Force on Multiple Axes Via Javascript?
Hi all. I am trying to apply a constant force to both the X & Y axes. However,
constantForce.force = Vector3.right * -var2;
constantForce.force = Vector3.up * var1;
wont work because it is thinking that I am overwriting the previous force when I try to implement it on another axis. How can I get around this? I have searched through the script reference and have been unable to fund anything, any help would be greatly appreciated. Thank you in advance.
Answer by aldonaletto · May 10, 2013 at 05:37 AM
Just add the forces and assign the result to constantForce.force:
constantForce.force = Vector3.right * -var2 + Vector3.up * var1;
I may be making a stupid mistake but even doing this, they dont work together. When I use only one at a time it works fine, but when I try to implement them together it doesn't work.
you are just making some unrelated silly mistake.
Add lines of code Debug.Log everywhete to find your problem.
Start with Debug.Log(" vars are " +var2 + " " + var1 );
you ay have for example silly values for some reason. cheers
The result is exactly the same. An alternative method without using constantForce is to add the forces separately inside FixedUpdate, like this:
function FixedUpdate(){
rigidbody.AddForce(-var2 * Vector3.right);
rigidbody.AddForce(var1 * Vector3.up);
}