transform.rotation = GameObject.Find(“Player”).transform.rotation;
GetComponent().AddRelativeForce(Vector3.forward * 10f);
If I change an object’s rotation and adding forward relative force to it, I find that the object’s local forward
has rotated accordingly but the force would still be applied in world forward position.
Can anyone please explain to me where I was wrong?
Not sure why this happens but as a workaround: if you want to be sure that the vector is rotated correctly you can instead just do it like this:
GetComponent<RigidBody>().AddForce(GameObject.Find("Player").transform.forward * 10f);
As a sidenote: Please avoid using any Find
methods as much as possible. Anything that relies on String comparisons will brick your game as soon as you rename something and is difficult to debug then.
It mostly is also not really friendly on performance.