- Home /
Relative rotation while moving on parent.
I have a transport ship that you can walk around on. I have an empty game object as a parent for both the "ship" (currently a flat cube as floor) and the unit. Selection and ordering works fine and you can order the unit around like in a normal RTS-game.
There are two problems however, first of all the movement isn't relative by nature so if I right-click somewhere and the entire ship moves, it'll try to move to the target location in world space, not relative to the ship. I've fixed this by saving a relative vector and applying it when I move. It works great.
The other problem is quite similar. If I order my unit to move away to the right and I at the same time rotate the entire ship, the unit will keep going right while it should in fact be going slightly angled to get to it's situation.
The obvious answer to this problem would be to just save a relative vector and move towards that one each time, but the problem becomes more complex since I'm combining this with A*. I've already set it up so the grid graph is updated as the possible interior walls on the ship move with the ship. The problem is that I cant just calculate a single relative vector and use it every time I move since I get a vector array that I don't want to recalculate each tick.
So what have I tried? Well, I feel like the correct approach would be to save the original rotation. Let's pretend Unity uses normal angles instead of Quaternions. Let's say the original path was calculated when the ship was rotated 200 degrees. Now in the update loop, I check the current ship rotation. Let's say it's changed to 250 degrees, I then rotate the current vector in the vector path by 50 degrees.
To translate that to Quaternions, I've been doing: "OriginalParentRot = Parent.transform.rotation" and then multiplying my relative vector with: "NewPosition = Quaternion.Inverse(OriginalParentRot) Parent.transform.rotation NewPosition"
So I basically undo the old rotation by using the Quaternion inverse and then I apply the new parent rotation. However, this doesn't work. It compiles fine but it doesn't seem to have an effect.
I feel like I'm rambling at this point. Am I approaching it the right way? Ideas?
Answer by Partel-Lang · Jul 14, 2013 at 08:53 PM
Why don't you just parent your units to the ship and use transform.localPosition and transform.localRotation for moving your units?
I'm sorry for taking so long, but it took some time before I had the time to try it out and when I did, I got some other weird bugs that took time to resolve.
To extend upon what you said (in case someone else finds this), this is what I did:
I saved the original position and original rotation. That way, I could translate the A* vector array into a bunch of vectors in local space (relative to the ship). This looks something like this:
currPath = path.vectorPath[currentWaypoint];
currPath -= OriginalParentPos;
currPath = Quaternion.Inverse(OriginalParentRot) * currPath;
The last row was the one I forgot at first. After you get the vector in local position, you can take it and subtract the units localposition from it to get a direction vector to move towards.