- Home /
rigidbody.AddForceAtPosition
I have this script:
var ForceX : float = 0;
var ForceY : float = 2;
var ForceZ : float = 0;
var positionx : float = 0;
var positiony : float = 0;
var positionz : float = 0;
function FixedUpdate () {
if(Input.GetKey(KeyCode.Tab)){
rigidbody.AddForceAtPosition(Vector3(ForceX,ForceY,ForceZ), Vector3(transform.position.x - positiony, transform.position.y - positionx, transform.position.z - positionz));
}
}
This is supposed to fin the position on the object, then addforce at that point. But the weird thing is, no matter what i set the values too, it doesn't work. Any ideas?
I tested this script and it worked for me. Have you checked if rigidbody rotation is enabled?
By the way, you can optimize your code:
var pos = Vector3(position.y,position.x,position.z);
rigidbody.AddForceAtPosition(...,transform.position-pos);
It reduces accesses to transform.position from 3 to 1. Another thing: positionx and positiony are swapped in your script, so I swapped them in the vector pos too; is this an intended behavior?
Answer by Waz · Jun 26, 2011 at 05:09 AM
There is nothing wrong with your code in principle. A force of 2 Newtons might be too small to have an effect. How heavy is the rigidbody? What is the friction with the surface is it resting upon? These are factors that will counter such a force.
O$$anonymous$$, I found the force i was applying was too small. But it doesn't, now, push it directly up- it tilts it at the same time. I might have to use animations ins$$anonymous$$d.
That's the purpose of AddForceAtPosition - it applies the force to a point other than the center of mass, and therefore imparts torque (twisting force). If you want to avoid twisting, just use rigidbody.AddForce.
i needed add force at point though, because addforce is NOT what i need. I need the force to be under a certain part of the object to lift it, but ins$$anonymous$$d of going straight up, it leans at the same time.
Why do you need it at a certain part of the object? That's the entire reason it is tilting. If you lift a chair by pushing up on one leg, it tilts. If you push at its center of mass, if lifts without tilting.
Depends where the center of mass is. If you want very specific physics results, use AddForce and AddTorque. It's normal (and good) to make the physics engine do what the player expects, and what looks right, regardless of the laws of physics, which for game designers are just guidelines ;-).
Your answer
![](https://koobas.hobune.stream/wayback/20220612122000im_/https://answers.unity.com/themes/thub/images/avi.jpg)