- Home /
Moving an object using a "direction" from another object
Hello everybody. I have been wrestling with this issue for the best part of two days so some guidance/assistance would be much appreciated.
In my 2D project I have an empty object which rotates around the Z axis randomly to represent the wind direction. This is my "windObject".
Under certain conditions I would like another separate "otherObject" to move in the direction towards the direction the wind is blowing from, but not towards the actual windObject.
As it is a 2D game I do not want the otherObject sprite to actually rotate at all.
So far my thinking has been:
1) I need to store the "windObject" rotation. Either as a Quaternion or as a Vector3 by converting the Quaternion: windObject.transform.rotation.eulerAngles;
(or do I just store the value in windObject.transform.rotation.z
??)
2) Using that rotation then work out the direction that my otherObject should move in (facing the wind).
3) Then use transform.translate
to move my otherObject in that direction for as long as I need to.
4) I cant just apply a rotation to my otherObject as I don't want it to do any rotating at all.
I have tried to sum it up in the picture below!
Any help with this would be really appreciated. These Quaternions and Vectors are starting to make my head spin...
Answer by NoseKills · May 01, 2015 at 01:42 PM
You can get a direction relative to a objects rotation by multiplying a Vector by the rotation. If you decide that the "direction" you are after with 0 degrees of rotation is "up", then you can get the rotated up-vector with windObject.transform.rotation * Vector3.up
.
Since you want the opposite direction, use Vector3 opposite = windObject.transform.rotation * Vector3.down
and use that to translate your moving objects.
Thank you very much, I didn't realise that you could multiply the quaternion by the vector but it is now working correctly for me so thats great :)