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 MachCUBED · May 24, 2012 at 07:08 PM · rotationguimathcompass

Get 360-degree rotation in on-screen directional arrow?

Hi guys,

I have the following code for an on-screen directional arrow:

function OnGUI() {

 target = GetClosestObject("Checkpoint", "Finish").transform;

if (target != null) { //Get the targets position on screen into a Vector3 targetPos = cam.WorldToScreenPoint (target.transform.position);

 //Get the middle of the screen into a Vector3
 screenMiddle = Vector3(Screen.width/2, Screen.height/2, 0); 
 
 //Compute the angle from screenMiddle to targetPos
 var tarAngle = (Mathf.Atan2(targetPos.x-screenMiddle.x,Screen.height-targetPos.y-screenMiddle.y) * Mathf.Rad2Deg)+90;
 
 //if (tarAngle < 0) tarAngle +=360;

 //Calculate the angle from the camera to the target
 var targetDir = target.transform.position - cam.transform.position;
 var forward = cam.transform.forward;
 var angle = Vector3.Angle(targetDir, forward);


 //If the angle exceeds 90deg inverse the rotation to point correctly
 if(angle<90)
 {
     transform.localRotation = Quaternion.Euler(-tarAngle,90,270);
 } 
 else 
 {
     transform.localRotation = Quaternion.Euler(tarAngle,270,90);
 }

 
 // Draw compass
 
 var centre = Vector2(dialPos.x + arrow.width / 2,  dialPos.y + arrow.height / 2);
 var savedMatrix = GUI.matrix;
 
 if (tarAngle != 0) 
     GUIUtility.RotateAroundPivot(angle, centre);
     
 GUI.DrawTexture(Rect(centre.x - (arrow.width / 2), centre.y - (arrow.height / 2), arrow.width, arrow.height), arrow);
 GUI.matrix = savedMatrix;
 }

}

It appears to have something that forces it to always run the other way after rotating 180 degrees. Because of that, it points the opposite way from where it should point when the player is in some orientations relative to the target. The solution is to allow 360-degree rotation, but I'm not sure how to enable it. Any ideas?

MachCUBED

Comment
Add comment · Show 5
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 MachCUBED · May 29, 2012 at 09:57 PM 0
Share

I edited the code to look like this:

function OnGUI() {

 target = GetClosestObject("Checkpoint", "Finish").transform;

if (target != null) { //Get the targets position on screen into a Vector3 targetPos = cam.WorldToScreenPoint (target.transform.position);

 //Get the middle of the screen into a Vector3
 screen$$anonymous$$iddle = Vector3(Screen.width/2, Screen.height/2, 0); 
 
 //Compute the angle from screen$$anonymous$$iddle to targetPos
 var tarAngle = ($$anonymous$$athf.Atan2(targetPos.x-screen$$anonymous$$iddle.x,Screen.height-targetPos.y-screen$$anonymous$$iddle.y) * $$anonymous$$athf.Rad2Deg);

 //Calculate the angle from the camera to the target
 var targetDir = target.transform.position - cam.transform.position;
 var forward = cam.transform.forward;
 var angle = Vector3.Angle(targetDir, forward);
 
 // Draw compass
 
 var centre = Vector2(dialPos.x + arrow.width / 2,  dialPos.y + arrow.height / 2);
 var saved$$anonymous$$atrix = GUI.matrix;
 
 if (tarAngle != 0) 
     GUIUtility.RotateAroundPivot(angle, centre);
     
 GUI.DrawTexture(Rect(centre.x - (arrow.width / 2), centre.y - (arrow.height / 2), arrow.width, arrow.height), arrow);
 GUI.matrix = saved$$anonymous$$atrix;
 }

}

And it still isn't rotating to the left side as needed. It's as if $$anonymous$$athf.Atan2 is always returning a positive value, and it needs to be set to negative if the target is to the left of the screen. What should I do?

avatar image MachCUBED · May 30, 2012 at 05:32 PM 0
Share

I tried to rewrite my code using this answer as a guide: http://answers.unity3d.com/questions/177383/trying-to-build-2d-compass.html

And came up with this code:

function OnGUI() {

 target = GetClosestObject("Checkpoint", "Finish").transform;

 if (target != null)
 {
     var dirVector = target.transform.position - player.transform.position;
     dirVector.y = 0; // remove the vertical component, if any
     var rot = Quaternion.FromToRotation(player.transform.position, dirVector);
     var angle: float; // angle is what we want
     var axis: Vector3; // but an axis variable must be provided as well
     angle = rot.eulerAngles.y;
 
     // Draw compass
     
     var centre : Vector2 = Vector2(dialPos.x + arrow.width / 2,  dialPos.y + arrow.height / 2);
     var saved$$anonymous$$atrix : $$anonymous$$atrix4x4 = GUI.matrix;
 
     GUIUtility.RotateAroundPivot(angle, centre);
     
     GUI.DrawTexture(Rect(centre.x - (arrow.width / 2), centre.y - (arrow.height / 2), arrow.width, arrow.height), arrow);
     GUI.matrix = saved$$anonymous$$atrix;
 }

}

It now rotates extremely slowly, with its rotation speed going up in unpredictable ways whenever I get close to a target. $$anonymous$$ore importantly, the arrow always points in a completely-different direction from where it should, and the result is that the code is even less useful than my old code. What am I doing wrong here?

$$anonymous$$achCUBED

avatar image Scribe · May 30, 2012 at 05:48 PM 0
Share

Is this a 2D game? do you have a screenshot showing the setup? as I'm having trouble understanding what you are trying to achieve from reading your code. :) thanks, Scribe.

avatar image MachCUBED · May 30, 2012 at 08:32 PM 0
Share

It's a 3d game. You can see an example screenshot (which shows the old code, not the code above) at http://twitpic.com/9r2drm

The arrow is supposed to target the closest hoop, and if I use the old code, it only points to the hoop if it's on the right of the player. This is because the old code doesn't appear to have 360-degree rotation capabilities.

avatar image MachCUBED · Jun 01, 2012 at 06:53 PM 0
Share

I tried looking to http://8bitmemories.blogspot.com/2011/12/compass-in-unity3d.html for help, and implemented his Compass.cs script as per his instructions. The pip didn't move, so now it's back to the same old DirectionArrow script.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Better control over the GUI components? 1 Answer

Rotating an object in level editor 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Unable to fully rotate GameObject on tap 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