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 bradleyb222 · Nov 29, 2016 at 10:12 PM · c#rotationtexturetransform

Idicator arrow flips upside down when player is below certain y axis. SOLVED!!!

I have been banging my head up against the wall for about a week now on how to resolve the indicator arrow flipping upside down when I drop below certain y axis during gameplay. I am fairly new to coding so please do go easy on me. Below is a copy of the code I am using that is attached to the objects that I want to identify. This works great until dropping below the y level. I have tried raising terrain to no avail. My landings are also all above 0. My player object is a ball with a camera attached to it which is the same camera (Main Camera) that I am using for this code. Thanks in advance for your help!

using UnityEngine; using System.Collections;

public class Newoffscreen : MonoBehaviour {

 //Attach this class to the GameObject you want the arrow to be pointing at.
  

     public Texture2D icon; //The icon. Preferably an arrow pointing upwards.
     public float iconSize = 50f;
     [HideInInspector]
     public GUIStyle gooey; //GUIStyle to make the box around the icon invisible. Public so that everything has the default stats.
     Vector2 indRange;
     float scaleRes = Screen.width / 500; //The width of the screen divided by 500. Will make the GUI automatically
     //scale with varying resolutions.
     Camera cam;
     bool visible; //Whether or not the object is visible in the camera.

     void Start () {
     visible = GetComponent<MeshRenderer> ().isVisible;

         cam = Camera.main; //Don't use Camera.main in a looping method, its very slow, as Camera.main actually
         //does a GameObject.Find for an object tagged with MainCamera.

     indRange.x = Screen.width - (Screen.width / 6);//this makes the indicator not stack 
     indRange.y = Screen.height - (Screen.height / 7);//this makes the indicator not stack 
     indRange /= 6.5f; //this keeps indicator close to center of screen

         gooey.normal.textColor = new Vector4 (0, 0, 0, 0); //Makes the box around the icon invisible.
     }

     void OnGUI () {
         if (!visible) {
         
             Vector3 dir = transform.position - cam.transform.position;
             dir = Vector3.Normalize (dir);
             //dir.y *= -1f;

             Vector2 indPos = new Vector2 (indRange.x * dir.x, indRange.y * dir.y);
             indPos = new Vector2 ((Screen.width / 2) + indPos.x,
                 (Screen.height / 2) + indPos.y);

         Vector3 pdir = transform.position - cam.ScreenToWorldPoint(new Vector3(indPos.x, indPos.y,
                 transform.position.z));
             pdir = Vector3.Normalize(pdir);

         float angle = Mathf.Atan2(pdir.x , pdir.y) * Mathf.Rad2Deg;

         GUIUtility.RotateAroundPivot(angle, indPos); //Rotates the GUI. Only rotates GUI drawn after the rotate is called, not before.
             GUI.Box (new Rect (indPos.x, indPos.y,  iconSize, iconSize), icon,gooey);
             GUIUtility.RotateAroundPivot(0, indPos); //Rotates GUI back to the default so that GUI drawn after is not rotated.
         }
     }

     void OnBecameInvisible() {
         visible = false;
     }
     //Turns off the indicator if object is onscreen.
     void OnBecameVisible() {
         visible = true;
     }
 UPATED:


After a little research and a lot of hours. I found a solution to this issue. Thanks to this post from Andeeeee back in 2009: https://forum.unity3d.com/threads/an-icon-pointing-to-an-offscreen-enemy.32075/

What I did was change the Vector3 dir to this.

Vector3 dir = cam.transform.InverseTransformPoint (transform.position); dir.y = 0.0f; dir.Normalize ();

That took the y factor out of the arrow rotation, or zeroed it out I should say.

Then I changed the Vector2 inPos to this:

Vector2 indPos = new Vector2 (indRange.x - dir.x,indRange.y - dir.z); indPos = new Vector2 (Screen.width / 2 + indPos.x, 100 + indPos.y);

In which as you can see the dir.y is replaced with dir.z.

Last but not least to keep the angle right I changed its y value to z also like this:

float angle = Mathf.Atan2(pdir.x , pdir.z) * Mathf.Rad2Deg;

You might have to adjust things to fit your needs but this should be a great start for everyone looking for this answer. Hope this helps out!

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

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

275 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 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 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 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 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 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 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 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

How to put limit for Transform.rotate in unity 2d 1 Answer

Clamp transform.rotation? 0 Answers

Movement and Direction script bugging out 1 Answer

Enemy not facing player when enter rotation orbit 0 Answers

How to rotate a camera slowly along a tween? 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