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 /
avatar image
0
Question by Ecnelis · Jun 19, 2013 at 03:12 PM · rotationtransformvector3pivot

How would I rotate an object relative to another without parenting?

The code below does the job, but as soon as I change the offsets to position the text it ends up rotating incrementally horrible until it's back to the original rotation. For example, if I set the offset to -0.9 it would move the text to the left of the object, when I've rotated said object 180 degrees the text is to the right of the object (or still left if you haven't changed the camera angle)

 private void AlignWithPlane(GameObject textObj, GameObject planeObj, float zOffset,
                             float xOffset = 0.0f, float yOffset = -0.0f)
                             //float xOffset = 0.05f, float yOffset = -0.9f)
 {
     textObj.transform.position = new Vector3(planeObj.transform.position.x + xOffset, // Away/towards plane
                                               planeObj.transform.position.y + yOffset, // Left/right plane
                                               planeObj.transform.position.z + zOffset); // Up/down plane
 
     textObj.transform.rotation = planeObj.transform.rotation;
 }
Comment
Add comment · Show 2
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 robertbu · Jun 19, 2013 at 03:29 PM 0
Share

Can you provide a drawing or picture of the alignment you are trying to achieve with these two object. There are multiple ways to solve this problem.

avatar image Ecnelis · Jun 19, 2013 at 04:30 PM 0
Share

I'll do you one better! Here's a video: http://youtu.be/L$$anonymous$$F7eNCPVa$$anonymous$$

2 Replies

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

Answer by robertbu · Jun 19, 2013 at 05:02 PM

The video is helpful, but I'm still not completely sure what you want the correct behavior to be. The issue I see is more about your offset than your rotation. It looks to me that you want a local offset relative to the plane, but you are calculating everything as world coordinates. You can handle this by:

 Vector3 v3Offset = new Vector3(xOffset, yOffset, zOffset);
 textObject.transform.position = planeObj.transform.TransformPoint(v3Offset);

Another way would be to use Transform.forward to set your position:

 textObj.transform.position = planeObj.transform.forward * offset;

Where 'offset' is some small float. Note for either solution you'll still have to assign the rotation as you do in the code above.

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 Ecnelis · Jun 19, 2013 at 05:17 PM 0
Share

Your first code snippet was exactly what I was after! Thank you very much. :) -hands e-beer-

avatar image
1

Answer by bubzy · Jun 19, 2013 at 04:42 PM

you might want to check Vector3.RotateAround()

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 Ecnelis · Jun 19, 2013 at 04:53 PM 0
Share

Would you be able to provide an example please? :) I'm assu$$anonymous$$g you meant Transform.RotateAround(). Every time I try to use that I get lost. :\

I need to be able to rotate with relativity to all three axis while maintaining the constraint (without parenting).

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

15 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

Related Questions

Instantiate GameObject towards player 0 Answers

Transform a Vector by a Quaternion 1 Answer

How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers

Multiple Fire on different launcher GameObjects 1 Answer

Smooth 90 degrees rotation on input . (infinite number of times) 1 Answer


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