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
1
Question by rickaz · Jan 20, 2013 at 08:23 PM · c#rotationvector3compass

Compass display 0 to +180 instead of 360°around.

I have to deal with the feared 180°-0° and back problem. I need the results of the angle in 360° degrees but have +180° 0° then back on the same side as the compass indicator :) makes it hard to find the target that way-lol

Also I noticed that the target indicator stays to only one side of the compass.

0° on the compass is 180° in the script, 180° on the compass is 0° in the script

All on the left side (180° to 360°) of the compass and never the full 0° to 360° around the compass perimeter.

 using UnityEngine;
 using System.Collections;
 
 public class Target_Indicator : MonoBehaviour
 {
     public Transform target; //Object that will be set in the Inspector.
     public Transform indicatorPlane; // Drag the indicator here
     public float angle = 0; //Create sting, Now we can see it in the inspector.
     
     void Update()
     {
         Vector3 targetDir = target.position - transform.position;
          Vector3 forward = transform.forward;
          targetDir.y = 0; // Remove any height difference
          angle = Vector3.Angle(targetDir, forward);
          indicatorPlane.eulerAngles = new Vector3(0, angle, 0);
     }
         
         void OnGUI()        
          {
             float displayValue = Mathf.Round(angle);//Change decimal to hole numbers.
             
             //Make a GUI label - position on sccreen - add "angle:" - display the value.
             GUI.Label(new Rect(30, 475, 150, 50), "angle: " + displayValue.ToString());
            }    
 }

I've seen other posts but the use a different structure than mine. One noted how to change to -180° 0° +180°, but again, I need 360°

It would stand to reason that when one works with rotation, the program whould display 0° to 360° or a option the of.

C# helllllp

Rick

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 Julien-Lynge · Jan 20, 2013 at 08:36 PM 0
Share

I'm sorry, but I don't understand a lot of what you've written here. Can you please explain what, for instance, the following line means:

I need the results of the angle in 360° degrees but have +180° 0° then back on the same side as the compass indicator

?

1 Reply

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

Answer by aldonaletto · Jan 20, 2013 at 08:41 PM

That's because Vector3.Angle always returns an unsigned value - you can't know whether it's positive or negative. You could use Vector3.Cross to find the angle polarity: the cross product results in a vector perpendicular to the two ones passed as arguments; considering that these vectors are perpendicular to world Y, you will have a positive Y value when the second vector is to the right, and negative when it's to the left (or the opposite - everybody gets confused about who's who in the cross product):

     ...
     angle = Vector3.Angle(targetDir, forward);
     angle *= Mathf.Sign(Vector3.Cross(forward, targetDir).y);
     indicatorPlane.eulerAngles = new Vector3(0, angle, 0);

This imposes the sign of the cross product's Y coordinate to the angle. If the sign is opposed to what you expect, swap the cross arguments (change to targetDir,forward).

Comment
Add comment · Show 3 · 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 Julien-Lynge · Jan 20, 2013 at 09:01 PM 2
Share

Nice answer!

BTW: This will return -180 to 180. To get 0 to 360, just add 180 to the result.

avatar image rickaz · Jan 20, 2013 at 09:44 PM 0
Share

That Worked all most, but it does not take account of player rotation. If I look at the target. all is good if I by pass the target all is good. but If the player rotates by 180°, It is not reflected if the player locally rotates. So how do we fix that.

I wish I had Aldo's brain at my screen :)

avatar image aldonaletto · Jan 21, 2013 at 11:29 AM 1
Share

To which object this script is attached? If you're using the First Person Controller, the script must be attached directly to it (not to the camera!). This code relies on the player being completely upright - if it may tilt, kill the Y component in the forward vector as well:

     Vector3 targetDir = target.position - transform.position;
     Vector3 forward = transform.forward;
     targetDir.y = 0; // Remove any height difference
     forward.y = 0;
     ...

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

11 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

Related Questions

Flip over an object (smooth transition) 3 Answers

slowly rotate a object *need quick fix* 0 Answers

transform.Rotate is rotating all axes, why? 0 Answers

Rotate a vector based on mouse x coordinates? 1 Answer

Trouble with Vector3.Angle() and RotateAround() 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