• 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
1
Question by stfx · Jun 20, 2011 at 12:44 PM · rotationnested

vector to viewpoint with nested rotation

I have a ship which has its own position and rotation and a parent transform object which also has its own position and rotation. This is essential for my game and works really well.

There is just one problem when I want to display the targeting reticle in front of the ship in third person view. This is the code:

 public class GUIController : MonoBehaviour
 {
     public Transform ReticleFront;

     const float frontReticleDistance = 50f;

     void FixedUpdate()
     {
         //front reticle: get reticle vector based on ship rotation
         Vector3 wantedPos = transform.TransformPoint(0, 0, frontReticleDistance);
         Debug.DrawLine(transform.position, wantedPos, Color.green);

         //front reticle: convert vector to viewpoint position as its a 2d texture
         ReticleFront.position = Camera.main.WorldToViewportPoint(wantedPos);
     }
 }

The Debug.DrawLine is drawn with the correct rotation/position but the viewport position of the reticle does somehow seem to ignore the rotation of the parent object.

I would be very thankful for any help. Greetings :)

Comment
Add comment · Show 6
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 aldonaletto · Jun 20, 2011 at 12:55 PM 0
Share

TransformPoint is affected by scale; could it be the problem? Anyway, I would use another approach:

   Vector3 wantedPos = transform.position+transform.forward*frontReticleDistance;

The result should be the same: wantedPos will be the world point located frontReticleDistance from the ship in its forward direction.

avatar image stfx · Jun 20, 2011 at 01:01 PM 0
Share

I dont see how scale should be a problem here. The calculated wantedPos is correct (your approach also has the same result as its actually the same calculation). The problem somehow lies in the WorldToViewportPoint with nested rotations (it ignores the parent rotation). Thanks nonetheless

avatar image Bunny83 · Jun 20, 2011 at 01:29 PM 0
Share

transform.TransformPoint will transform a point from local space into world space and it considers all parents positions / rotations / scales.

I don't get how your point is wrong positioned? A point is a point and it has no rotation. It's just a position. WorldToViewportPoint transforms the point in world coordinates into viewport coordinates. How do you draw your reticle in 2d?

avatar image PaulUsul · Jun 20, 2011 at 01:31 PM 0
Share

I find it a little hard to imagine, can you post a picture of what it looks like.

avatar image sneftel · Jun 20, 2011 at 01:31 PM 3
Share

When you say "ignores the rotation", what do you mean? The reticle is always just in the center of the viewport? It translates with the ship, but is always (say) to the left of it on the viewport, even when the ship rotates?

BTW, it's a little odd to be doing this in FixedUpdate... LateUpdate would seem more appropriate, given that the position is affected by other objects' positions but does not itself affect other objects.

Show more comments

1 Reply

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

Answer by sneftel · Jun 20, 2011 at 02:06 PM

If changing to LateUpdate fixes things, that's probably because you've got some other behavior also messing with the reticle's transform (LateUpdate is one of the last functions to run before rendering).

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 stfx · Jun 20, 2011 at 02:16 PM 0
Share

Wow you sir are awesome. I accidently had the targeting reticle nested under the ships parent transform and therefore it was not working. So it now even works in Update() or FixedUpdate() though I am using LateUpdate() now due to your explanation. Thanks a bunch :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Smoothly bend around a point - rotation problem. 1 Answer

Third person follow camera on a sphere 1 Answer

Rotate character to the moving direction problems? 2 Answers

Rotate object based on rotation of SteamVR Controller? 2 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