Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Siretu · Jul 14, 2013 at 08:41 AM · rotationquaternionrelative

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Siretu · Jul 16, 2013 at 09:19 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rotation of child object inside another simultaneously rotated object 1 Answer

made a camera relative movement, but it works weird when player rotates. 0 Answers

How to Rotate Relative To World 1 Answer

unwanted rotation on the y and z axes 1 Answer

Connecting transform with Vector3 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges