- Home /
How does addrelativeforce work?
I took 2 objects a sphere and a capsule. The script attached to sphere:
public class Sphere : MonoBehaviour
{
public Transform capsule;
void Start()
{
this.transform.LookAt (capsule);
this.GetComponent<Rigidbody> ().AddRelativeForce (Vector3.forward * 2000f);
}
}
The sphere turns towards the capsule but moves along it's previous z-axis. Shouldn't the sphere move towards the capsule?
Answer by aurelioprovedo · Dec 18, 2017 at 09:22 AM
In my Unity, the sphere is rotating correctly, and moving towards the capsule in its new z-axis. But only once, at the Start.
AddRelativeForce is a helper method for you to add forces to rigidbodies in their local space. You can always achieve this by using AddForce instead, but you would have to add some extra code to it.
Thanks for the reply. As you can see in the image when I press play, the local coordinates of the sphere are shifting to face the capsule. But it is still moving along it's previous z-axis. Why is that?
Damn, I can't reproduce it... :-/
I copied your code and did what you say, in your same Unity version, and in my scene it works as expected. $$anonymous$$aybe try it in a new empty project...?