Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Placus_Brutus · Mar 02, 2016 at 01:31 PM · rotationuigameobjectvector3vector2

Make RawImage on canvas look at gameObject

I have a RawImage that I'm treating as a sort of "compass" that will rotate to point at a gameObject. My problem is, the RawImage is part of my canvas and whenever I rotate it to look at the gameObject, it rotates on all three axises and when I lock it to just the Z axis, it still doesn't point to the object. I believe it's a problem with going from Vector3 (gameObject's position) to Vector2 (RawImage's position), but I still can't get it to point properly.

Thank you!

Comment
Add comment · Show 3
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 Glurth · Mar 02, 2016 at 04:19 PM 0
Share

I think we need some more details. For example, how is the compass supposed to represent a 3d direction? A Real compass cannot: It can give you a direction, but that direction lies on the plane of the compass. So, if you held the compass flat, it would not be able to give you the elevation.

Along this line of though, I would have expected you to convert your Vector3 direction, into an ANGLE (rather than a Vector2).

Assu$$anonymous$$g the compass lies horizontally I would do this as follows:

Simply ignore you elevation-axis, and convert the object direction into a Vector2 that represents a purely-horizontal direction. (only copy the non, elevation coordinates)

Then perform the angle computation on that Vector2 (e.g. compasAngle=Vector2.Angle(noElevationVector2,Vector2.up) )

The angle that results would be how much to rotate your image: e.g. image.transform.localRotataion = Quaternion.Euler(0,0,compasAngle);

note: If you want a '$$anonymous$$i-map' you could also scale the resultant noElevationVector2, to the $$anonymous$$i-map view size, if you wanted to draw a dot for the object on the image.

Edit: note- the coordinates I used in the examples, might not be appropriate for your setup.

avatar image Placus_Brutus Glurth · Mar 03, 2016 at 04:09 AM 0
Share

Thank you. Your assumption would be correct. I'm making a top down game and I'm using the image to point in the direction that the objective is in. Let's say the objective is to go get an item, I'm trying to get the "compass" to point in the direction of the item so that the player knows which way to go.

EDIT: I tried the code that you provided, and it works to an extent. It is only accurate when the object is on the left side of the image. If the object is to the right of the image, however, the arrow mirrors what it should be, pointing North-West ins$$anonymous$$d of North-East. It also only updates when the objective is moved, not when the camera moves (the player moving around the map).

avatar image Glurth Placus_Brutus · Mar 03, 2016 at 09:49 AM 0
Share

" only updates when the objective is moved, not when the camera moves (the player moving around the map)."

when I mentioned the "object direction" above, this can be interpreted to mean a few different things, it could be the object's absolute position, or it could be the objects position RELATIBE to the player. To get the direction (and distance) from the player to the object (relative), simply subtract position vectors e.g. playerToObjectVector3 = object.transform.position - player.transfrom.position

regarding it flipping around the wrong way, I'm not quite sure.. but perhaps I use the wrong relative direction when I suggested Vector2.up? It's also possible the Vector2.Angle function is only returning positive values. $$anonymous$$aybe try using Atan2, ins$$anonymous$$d, to get the angle:

 compasAngle=$$anonymous$$athf.Atan2(noElevationVector2.x,noElevationVector2.y);


0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotat a GameObject and than shoot it in the New (x) direction 1 Answer

How to properly set rotation constraints in the positive and negative direction? 0 Answers

How to get a vector 3 to vector 2 - c# 1 Answer

Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer

New Vector2 won't take effect when called from another script in the same GameObject. 0 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