Search Unity

4.6 UI Slider WorldToScreenPoint - Not desired effect, (working correctly).

Discussion in 'Immediate Mode GUI (IMGUI)' started by shahan, Oct 7, 2014.

Thread Status:
Not open for further replies.
  1. shahan

    shahan

    Joined:
    Jan 27, 2014
    Posts:
    25
    So I've outlined the problem the best I can in this video:



    It's when I'm not looking at the WorldToScreenPoint's object of interest... How do I deal with it being out of the canvas/camera view?

    Is it the UI canvas I should be concerned with? or...

    Here is the script on the Slider:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class placeEnemyHealthBar : MonoBehaviour
    6. {
    7.   public GameObject target;
    8.   public int verticalOffset = 15;
    9.   RectTransform thisRect;
    10.   public Vector3 wantedPos;
    11.  
    12.   // Use this for initialization
    13.   void Start()
    14.   {
    15.     thisRect = GetComponent<RectTransform>();
    16.   }
    17.  
    18.   // Update is called once per frame
    19.   void Update()
    20.   {
    21.  
    22.     if (target != null)
    23.     {
    24.       wantedPos = Camera.main.WorldToScreenPoint(target.transform.position + new Vector3(0,verticalOffset,0));
    25.       thisRect.anchoredPosition = wantedPos;
    26.     }
    27.  
    28.     //an object that is a direct child of the Canvas, has its local coordinates the same as its screen coordinates
    29.     //so, get the RectTransform, set its anchoredPosition to WorldtoScreenPoint of the obejct in question
    30.  
    31.   }
    32. }
    33.  
    Here is the script on the enemy object transform:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class moveEnemyMechanics : MonoBehaviour
    6. {
    7.  
    8.   public Text onScreenInfo;
    9.   public Slider onScreenEnemyHealth;
    10.   public int moveEnemyHealth = 10;
    11.   public float distanceFromPlayer;
    12.   public ParticleSystem movPart;
    13.   public GameObject ammoPickupPF;
    14.  
    15.   void OnMouseOver()
    16.   {
    17.     if (distanceFromPlayer < 100)
    18.     {
    19.       onScreenInfo.text = "HEALTH " + moveEnemyHealth;
    20.       onScreenInfo.CrossFadeAlpha(1, 1, true);
    21.  
    22.       onScreenEnemyHealth.value = moveEnemyHealth;
    23.       GameObject.Find("Enemy Health").GetComponent<placeEnemyHealthBar>().target = gameObject;
    24.     }
    25.   }
    26.  
    27.   void OnMouseExit()
    28.   {
    29.     onScreenInfo.CrossFadeAlpha(0, 1, true);
    30.   }
    31.  
    32.   // Use this for initialization
    33.   void Start()
    34.   {
    35.  
    36.   }
    37.  
    38.   // Update is called once per frame
    39.   void Update()
    40.   {
    41.     distanceFromPlayer = Vector3.Distance(transform.position, GameObject.Find("Player").transform.position);
    42.  
    43.     if (moveEnemyHealth <= 0)
    44.     {
    45.       var ammoDrop = Instantiate(ammoPickupPF, transform.position, transform.rotation) as GameObject;
    46.       ammoDrop.rigidbody.AddRelativeForce(0, 2000, 0);
    47.       onScreenInfo.CrossFadeAlpha(0, 1, true);
    48.       Instantiate(movPart, transform.position, transform.rotation);
    49.       Destroy(gameObject);
    50.     }
    51.   }
    52.  
    53.   void OnCollisionEnter(Collision other)
    54.   {
    55.     if (other.gameObject.tag == "PlayerBullet")
    56.     {
    57.       moveEnemyHealth--;
    58.     }
    59.   }
    60.  
    61. }
     
  2. Kuothe

    Kuothe

    Joined:
    Aug 14, 2013
    Posts:
    18
    This is how we deal with it in Twin Souls: The Path of Shadows.

    Code (CSharp):
    1. bool UpdateInterfacePos()
    2.     {
    3.         sw2 = Screen.width * 0.5f;
    4.         sh2 = Screen.height * 0.5f;
    5.  
    6.         //screenPos = cam.WorldToScreenPoint(enemyHead.position + offset);
    7.         screenPos = Camera.main.WorldToScreenPoint(enemy.position + offset + new Vector3(0,2.0f,0));
    8.  
    9.         screenPos.x -= sw2;
    10.         screenPos.y -= sh2;
    11.         distEnemy = Vector3.Distance(cam.transform.position, enemy.position);
    12.  
    13.         //Comprobar si la interfaz queda dentro o fuera de la pantalla
    14.         t = screenPos.y > sh2 - id;
    15.         b = screenPos.y < -sh2;
    16.         l = screenPos.x < -sw2 + ir;
    17.         r = screenPos.x > sw2 - ir;
    18.  
    19.         offScreen = t || b || l || r || screenPos.z < 0.0f;
    20.  
    21.         //if (canvasGroupTools.CurrentAlpha == 0.0f) return false;
    22.  
    23.         //fuera de la pantalla
    24.         if (offScreen)
    25.         {
    26.             //Calculamos posicion relativa al player de forma radial
    27.             Vector3 cam2Enemy2D = enemy.transform.position - cam.transform.position;
    28.             cam2Enemy2D.y = 0.0f; cam2Enemy2D.Normalize();
    29.             Vector3 camForward2D = cam.transform.forward;
    30.             camForward2D.y = 0.0f; camForward2D.Normalize();
    31.  
    32.             //height: aqui se tiene en cuenta la direccion en el plano XZ
    33.             outH = -Vector3.Dot(camForward2D, cam2Enemy2D) * Vector3.Dot(cam.transform.forward, Vector3.up);
    34.             //width: en XZ (solo)
    35.             outW = Mathf.Sqrt(1.0f - outH * outH) * Vector3.Dot(cam.transform.right, cam2Enemy2D);
    36.  
    37.             Vector3 player2Enemy3D = enemy.transform.position - enemy.GetComponent<EnemyDataScript>().getPlayer().transform.position;
    38.             player2Enemy3D.Normalize();
    39.  
    40.             //Esto sirve para que la height tenga en cuenta la direccion en el plano XZ y YX(local de la camara). Asi la brujula emula un comportamiento 3D
    41.             vFactor = Vector3.Dot(player2Enemy3D, Vector3.up) * 2.5f * (1.0f - Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.up)));
    42.             vFactor = Mathf.Clamp(vFactor, -1.0f, 1.0f);
    43.             outH += vFactor;
    44.  
    45.             //Ahora viene cuando pegamos la pos a las esquinas de la pantalla
    46.             angle = Vector3.Angle(camForward2D, cam2Enemy2D);
    47.  
    48.             if (angle > 45.0f && angle < 135.0f)
    49.             {
    50.                 //laterales
    51.                 if (outW > 0.0f) outW = sw2 - ir*scale;
    52.                 else outW = -sw2 + ir * scale;
    53.  
    54.                 outH = outH * sh2;
    55.                 if (outH > sh2 - id * scale) outH = sh2 - id * scale;
    56.                 else if (outH < -sh2) outH = -sh2;
    57.             }
    58.             else
    59.             {
    60.                 //partes superior e inferior
    61.                 if (outH > 0.0f) outH = sh2 - id * scale;
    62.                 else outH = -sh2;
    63.  
    64.                 outW = outW * sw2;
    65.                 if (outW > sw2 - ir * scale) outW = sw2 - ir * scale;
    66.                 else if (outW < -sw2 + ir * scale) outW = -sw2 + ir * scale;
    67.             }
    68.  
    69.             rectT.anchoredPosition = new Vector2(outW, outH);
    70.         }
    71.         else //dentro de la pantalla
    72.         {
    73.             rectT.anchoredPosition = screenPos;
    74.             //canvasGroupTools.SetDesiredAlpha(0.0f);
    75.             return false;
    76.         }
    77.         return true;
    78.     }
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please post in the correct section.

    This section is for extensions and the immediate mode UI; OnGUI.
     
Thread Status:
Not open for further replies.