Search Unity

GUIText hover over a player issue

Discussion in 'Scripting' started by NikoBusiness, Oct 18, 2014.

  1. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    PLEASE DONT IGNORE IT!



    hey guys i have this script where guitext shows up over a character displaying his name and i have an issue
    proof guitext.jpg
    this is working good when im seeing the player but when im not seeing the player,if i am close to him guitext is not showing up(thats what i want)but if i start to get away guitext shows up :/ and i cant understand why
    proof guitext.jpg

    this is the script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class guitextfollow : MonoBehaviour {
    6.     public Transform target;
    7.     public Vector3 offset= Vector3.up;
    8.     public bool clampToScreen = false;
    9.     public float clampBorderSize = 0.05f;
    10.     public bool useMainCamera = false;
    11.     public Camera cameraToUse;
    12.     private Camera cam;
    13.     private Transform thisTransform;
    14.     private Transform camTransform;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         thisTransform = transform;
    19.         if (useMainCamera)
    20.             cam = Camera.main;
    21.         else
    22.         {
    23.             GameObject obj = GameObject.FindWithTag("myCamera");
    24.             cameraToUse = obj.GetComponent<Camera>();
    25.             cam = cameraToUse;
    26.         }
    27.         camTransform = cam.transform;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update () {
    32.         if(cameraToUse == null)
    33.         {
    34.             GameObject obj = GameObject.FindWithTag("myCamera");
    35.             cameraToUse = obj.GetComponent<Camera>();
    36.             cam = cameraToUse;
    37.             camTransform = cam.transform;
    38.         }
    39.  
    40.         if(clampToScreen) {
    41.             var relativePosition = camTransform.InverseTransformPoint(target.position);
    42.             relativePosition.z = Mathf.Max(relativePosition.z, 1.0f);
    43.             thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
    44.             thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x,clampBorderSize, 1.0f-clampBorderSize),thisTransform.position.z);
    45.  
    46.  
    47.         }
    48.         else
    49.         {
    50.             thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
    51.         }
    52.  
    53.     }
    54. }
    55.  
    56.  
    these are the settings
    proof guitext.jpg
     
    Last edited: Oct 19, 2014
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I don't see anything in that code designed to enable/disable the gui text...
     
  3. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    Dameon I'm trying to understand why is that happening.I think that doesn't need to disable it since its not showing up when I don't see the target but as I get away from the other player the GUI shows up
     
  4. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    NikoBusiness likes this.
  5. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    Kogar hi , man I can't understand why its working when I'm near the player and it it starts functioning incorrect when I I'm moving far from the player...? I'm near the player and I don't see it ,I don't see the gui, which is good but if I move far from the player GUI changes position and I see it
     
  6. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Weird. I tried your code and the guitext is always shown. But since there is nothing which determines if the guitext should be shown/hidden it is working as expected.
     
  7. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    check this out i captured a video
     
  8. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Since i can't reproduce it i am out of ideas.
    In your video there are multiple Argument out of range exception error.
    I would try it on an empty new scene with only the player characters and a terrain/plane. Maybe one of your other scripts is interfering. Or do you have multiple cameras? Maybe your script can't decide which one to use.
     
  9. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    These exceptions are networking exceptions yes there are multiple cameras but only one is active and the script chooses the camera that is tagged with mycamera. Others are tagged with 'camera,' obj is local variable maybe if I make it global it will be fixed I don't know when I get on my hands to my PC I will try it and something ellse ... Can you explain to me what clipplane means?? What it does???
     
  10. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    You can forget the clipplane value.
    From your video it seems like your guifollow(not on your playercharacter) script doesn't get the correct screenposition after it was in the background and you rotate towards that guifollow which is near of your character.
    Local variables are good if there is no need to access them from another script.
    So my advice is to test only the guifollow script on an empty new scene. If it is still not working than you can try to rebuild the script.
    But as i said i am out of ideas. Your script should work and was working in my test scene as expected.
     
  11. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    OK thanks for your help!if I fix it I'll post here
     
  12. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    I tested it on a clean scene... Does the same thing so is there a possibility to make something like if you don't see the character then disable the GUI gameobject
     
  13. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    well that is my last bit of possible help
    you can try it again with the original http://wiki.unity3d.com/index.php/ObjectLabel code without your added myCamera distinction. Then you will know if the error comes from your guifollow script or if some other script is intervening.
    In case another script is intervening you have to look what other script are active and deactivate each of them. Reactivate them one step at a time.


    Otherwise here is some more stuff that i have now seen that can be done in your guifollow script.

    First you have deleted too much in "if(clampToScreen)". Have you used clamp to screen in your video? From your settings images you weren't using that value
    you have :
    Code (csharp):
    1. thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x,clampBorderSize, 1.0f-clampBorderSize),thisTransform.position.z);
    instead of:
    Code (csharp):
    1. thisTransform.position = new Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0f - clampBorderSize), Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0f - clampBorderSize), thisTransform.position.z);
    There is unnessary code in update(). You already do that in start()
    Code (csharp):
    1. if(cameraToUse == null)
    2.      {
    3.        GameObject obj = GameObject.FindWithTag("myCamera");
    4.        cameraToUse = obj.GetComponent<Camera>();
    5.        cam = cameraToUse;
    6.        camTransform = cam.transform;
    7.      }
    with that code in update you would always use "myCamera" since cameraToUse would be null if useMainCamera is true in start()
     
  14. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The problem should be quite obvious:
    Code (CSharp):
    1. thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
    returns a Vector3 which has inappropiate values for your use when your object is behind the camera.

    A simple fix may be the following:
    Disable your guiText whenever transform.position.z is less than or equals zero, which kind of means disable the guiText when it's 'behind' my camera (while behind refers to the opposite direction of the look-direction).

    You may need to adjust the other part of your code (when clamping is enabled). Haven't tested that one.
     
  15. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Hmm yes that should be it.
    I always thought frustum culling ( http://forum.unity3d.com/threads/gameobject-is-in-view-of-camera-but-not-visible.248144/ ) would automatically deactivate/reactivate (mesh and other)renderers. But the Update() should continue to run and try to place the guitexture somewhere in a nonsensical visible space which would hinder a deactivation.

    transform.position.z is good but it still needs to be set in relation to the camera.
    in the script reference is a good example.
    http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
    Code (csharp):
    1. // Calculate the transform's position relative to the camera. var cam = Camera.main.transform;
    2. var cameraRelative = cam.InverseTransformPoint(transform.position);
    3. if (cameraRelative.z > 0)
    4. print ("The object is in front of the camera");
    5. else
    6. print ("The object is behind the camera");
    Well thats enough from me. @NikoBusiness can try the new stuff out and tell if he could progress.
     
  16. NikoBusiness

    NikoBusiness

    Joined:
    May 6, 2013
    Posts:
    289
    Hey guys thanks for your help this should work definitely .I need to learn linear algebra xD