• 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 10:13 AM · c#variableangle

float "angle" should be a variable, not a object.

If float "angle" should be a variable. why is it Debug.Log(); as a object that I can not use. The script should take the vector3s and return the angle of the 2 objects. Then round to nearest hole number and display it on screen, It failed. To remove public float angle =0; gives multiple errors.To use the script as is: Dose nothing. some times it said the angle is populated but value is never used.

 using UnityEngine;
 using System.Collections;
 
 public class Target_Indicator : MonoBehaviour
 {
     public Transform target; //values that will be set in the Inspector.
     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;
          float angle = Vector3.Angle(targetDir, forward);
     }
         
         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());
            }    
 }

So what is going on with the value "angle" that I can not use it to display in the inspector or on screen.

Comment
Add comment
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

1 Reply

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

Answer by aldonaletto · Jan 20, 2013 at 10:26 AM

You're storing the angle calculated in Update in a temporary variable angle - the variable angle you've defined at the beginning isn't ever affected. Temporary variables are those declared inside a function: they exist only while the function is running, and are destroyed upon return.
To solve the problem, just remove the float declaration before angle in Update:

 void Update()
 {
     Vector3 targetDir = target.position - transform.position;
     Vector3 forward = transform.forward;
     angle = Vector3.Angle(targetDir, forward); // no float declaration here!
 }

EDITED: If you're using planes for the indicator and compass, set the indicator Y angle to the angle found. You should also remove height differences from the direction vector to keep it strictly horizontal, like below:

 public Transform indicatorPlane; // drag the indicator here

 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 = Vector3(0, angle, 0);
 }

But this isn't the best way to display an on-screen compass - usually this is done in OnGUI: draw the compass texture, rotate the gui matrix with GUIUtility.RotateAroundPivot, draw the indicator texture, then restore the gui matrix - something like this:

 public Rect rCompass = new Rect(30, 400, 80, 80); // compass position/size
 public Texture2D compass;
 public Texture2D indicator;

 void Update()
 {
     Vector3 targetDir = target.position - transform.position;
     Vector3 forward = transform.forward;
     targetDir.y = 0; // remove any height difference
     angle = Vector3.Angle(targetDir, forward);
 }

 void OnGUI(){
     GUI.DrawTexture(rCompass, compass); // draw the compass
     Matrix4x4 curMatrix = GUI.matrix; // save cur matrix
     GUIUtility.RotateAroundPivot(angle, rCompass.center); // rotate gui matrix
     GUI.DrawTexture(rCompass, indicator); // draw indicator
     GUI.matrix = curMatrix; // restore matrix (no rotation)
 }


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 rickaz · Jan 20, 2013 at 01:42 PM 0
Share

O$$anonymous$$ - that worked as it should. But my script is only half way there.

Plan:

object 1 - A standard prefab cube as a target to witch the script will apply.

object 2 - The player, after all, the indicator will help him find his goal.as of now, the angle is from the object 1 to player location.

object 3 - A plane with transparency texture symbolizing an arrow on screen, using a other stationary objects created as a HUD with it's own off player camera. (set under the terrain.)

I need to take a angle from player to target, get the result. Then convert that result to rotational movement in degree, and apply it to rotate the indicator's plane.

alt text

I do need help, As this gets more complex, with my limited understanding of C#

example.jpg (135.0 kB)
avatar image aldonaletto · Jan 20, 2013 at 02:39 PM 0
Share

Take a look at my answer: I edited it to show how to rotate the indicator plane, and also how to do it in OnGUI, without any auxiliary camera.

avatar image rickaz · Jan 20, 2013 at 03:28 PM 0
Share

Assets/Scripts/Target_Indicator.cs(16,38): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

 using UnityEngine;
 using System.Collections;
 
 public class Target_Indicator : $$anonymous$$onoBehaviour
 {
     public Transform target; //values 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 = Vector3(0, angle, 0);
     }
         
         void OnGUI()        
          {
             float displayValue = $$anonymous$$athf.Round(angle);//Change decimal to hole numbers.
             
             //$$anonymous$$ake a GUI label - position on sccreen - add "angle:" - display the value.
             GUI.Label(new Rect(30, 475, 150, 50), "angle: " + displayValue.ToString());
            }    
 }

this line has the error at the = sign I fixed it with adding new

indicatorPlane.eulerAngles = Vector3(0, angle, 0); TO

indicatorPlane.eulerAngles = new Vector3(0, angle, 0);

The reason I set a downward facing camera is theirs multi plane objects that rotate at different rates.

  1. compass body - plane with texture - stationary

  2. compass needle - plane with texture - eulerAngles to player rotation.

  3. target indicator - plane with texture - to point to goal(the box in the image)

Then I have the cross hair ongui

I not sure if I can con do all that by gui matrix method. coding is not my strong suite.

I also see the I have to deal with the feared 180-0-180 problem. I need the results of the angle in 360 degrees but have +180 0 +180 :) 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.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using a script as a member variable in another script. 2 Answers

Editing a variable from another script on collision 3 Answers

Problem with translating C# variable to JS. 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