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 /
avatar image
1
Question by daviddickball · Aug 30, 2015 at 05:44 PM · rotationwaypointpointerarrowcompass

Make UI compass point to 3D object

I want a compass needle to point to a target object offscreen. The compass is an upwards-facing arrow sprite in a Canvas, so will rotate on the Z-axis. The rest of the game is 3D.

From other answers here on UA I've made this calculation:

 var dir : Vector3;
 var angle : float;
 var arrow: GameObject;
 
 dir = target.transform.position - transform.position;
 angle = Mathf.Atan2(dir.x, dir.z);
 arrow.transform.eulerAngles = Vector3(0, 0, angle);

...but this doesn't give the correct angle. Could anyone give me a pointer please?

Comment
Add comment · Show 1
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 daviddickball · Aug 31, 2015 at 08:32 PM 0
Share

@scribe of course, I've missed out an important part! $$anonymous$$ake this into an answer and I'll accept it!

4 Replies

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

Answer by Scribe · Aug 31, 2015 at 06:16 PM

Mathf.Atan2(dir.x, dir.z) returns the rotation in Radians, so you have to multiply it by the conveniently named 'Mathf.Rad2Deg'

 angle = Mathf.Atan2(dir.x, dir.z)*Mathf.Rad2Deg;
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 fermmmm · Sep 07, 2017 at 09:35 PM 0
Share

This worked for me:

 var targetPosLocal = Camera.transform.InverseTransformPoint(targetObjectPosition);
 var targetAngle = -$$anonymous$$athf.Atan2(targetPosLocal.x, targetPosLocal.y) * $$anonymous$$athf.Rad2Deg - 90;
 ArrowUIObject.eulerAngles = new Vector3(0, 0, targetAngle);




avatar image
3

Answer by SuperJaques · Sep 01, 2015 at 01:30 PM

Put this script to arrow object and drop your target to "toFollow" public variable or ref it some other way.

using UnityEngine; using System.Collections;

public class Follow : MonoBehaviour {

public GameObject toFollow;

void Update () {

Vector3 v = toFollow.transform.position;

//z is meaningless

v.z = 0;

//arrow always looks forward so it will show correctly to viewer, and world-up changes the rotation

transform.LookAt(transform.position+transform.forward,v-transform.position);

} }

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 jessesloan1 · Sep 06, 2020 at 02:58 PM 0
Share

I have a compass needle sprite pointing upward, parented to a UI canvas that is set to Screen Space Camera. So, wherever the canvas/camera goes the sprite goes along for the ride and keeps it's relative position. I have been searching for hours for some sort of LookAt2D equivalent. I tried the above code and it works famously! I up voted this answer. Here is the code from @SuperJaques in a bit more readable format.

 public class CompassPointer : $$anonymous$$onoBehaviour
 {
     public GameObject toFollow;
 
     void Update()
     {
         Vector3 v = toFollow.transform.position;
 
         //z is meaningless
         v.z = 0;
 
         //arrow always looks forward so it will show correctly to viewer, and world-up changes the rotation
         transform.LookAt(transform.position + transform.forward, v - transform.position);
     }
 }
avatar image
1

Answer by fermmmm · Sep 07, 2017 at 09:37 PM

This worked for me:

 var targetPosLocal = Camera.transform.InverseTransformPoint(targetObjectPosition);
 var targetAngle = -Mathf.Atan2(targetPosLocal.x, targetPosLocal.y) * Mathf.Rad2Deg - 90;
 ArrowUIObject.eulerAngles = new Vector3(0, 0, targetAngle);
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 citronovafuska · Feb 14 at 09:59 AM 0
Share

This is amazing, finally works without any issues. To anyone reading this, be sure to change that -90 in "Mathf.Rad2Deg - 90;" to your specific rotation required by sprite.

avatar image
0

Answer by mBakr · Dec 08, 2017 at 04:31 AM

Yet another solution:

 Vector3 dir = transform.InverseTransformDirection(target.position - transform.position);
 float angle = Mathf.Atan2(-dir.x, dir.z) * Mathf.Rad2Deg;
 uiTrans.eulerAngles = new Vector3(0, 0, angle);
Comment
Add comment · 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

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

35 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 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 avatar image avatar image avatar image

Related Questions

Always rotate the camera to be stood close to upright. 1 Answer

Problem with NPC rotating around a waypoint 3 Answers

rotate object to waypoints rotation 1 Answer

Waypoint System Does Not Rotate Enemy? 2 Answers

Add force to player at a certain z angle in 2D 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