Search Unity

What is the proper use of WorldToScreenPoint?

Discussion in 'Scripting' started by asdx, Jun 19, 2013.

  1. asdx

    asdx

    Joined:
    Jun 3, 2013
    Posts:
    18
    I can't seem to find the answer I need anywhere on the forum or answers.. or I'm just too dumb to understand. I keep getting errors when trying to use this function (WorldToScreenPoint). I tried attaching the scripting example provided by Unity to a Cube. Code:
    Code (csharp):
    1.    
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour {
    6.     public Transform target;
    7.     void Update() {
    8.         Vector3 screenPos = camera.WorldToScreenPoint(target.position);
    9.         print("target is " + screenPos.x + " pixels from the left");
    10.     }
    11. }
    12.     }
    In this form I get:
    I tried changing the c letter to capital C, then I get this error:
    Tried putting:
    Code (csharp):
    1. Vector3 screenPos = Camera.main.WorldToScreenPoint(target.position);
    When I run the game it says:
    So how do I use this function ?? :(
     
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Camera.main was ok .. im sure you probably have not set the target variable to have a value
     
  3. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Not sure why Camera.main does not work. There are many ways to achieve what you need. Here is one:

    1. Create a property camera in your example class.

    Code (csharp):
    1.     public Camera camera = null;
    2.  
    2, In Inspector drag your main camera to that property.. Now you can use it from Update() method
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This isn't ideal because 'camera' is already a member of Component (and GameObject) which refers to the camera attached to 'this' object. So if you simply attach a Camera to the GameObject in question then there is no need for the additional declaration or if you would rather assign via Inspector then a different variable name is in order.
     
  5. asdx

    asdx

    Joined:
    Jun 3, 2013
    Posts:
    18
    Can I make it work with transform.position instead of target?
    Can someone please post a code? (PS I've tried even declaring public Camera camera but it doesn't work)
     
  6. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Agreed with a different variable name point. I was just trying to keep the name to be the same as in the original example. As far as attaching it as a component - I don't think it's what asdx wants to achieve here.
     
  7. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    asdx - after you declared a public Camera property, did you drag your camera into that property in the Inspector?
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Here's a quick working example that will throw an error up if things aren't set up correctly.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.     public Transform target;  // set via Inspector
    7.     void Awake()
    8.     {
    9.         if (target == null) Debug.LogError("Set target variable in Inspector!");
    10.         if (Camera.main == null) Debug.LogError("There is no camera tagged as 'Main Camera'!");
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         Vector3 screenPos = Camera.main.WorldToScreenPoint(target.position);
    16.         Debug.Log("Screen position is: " + screenPos);
    17.     }
    18. }
    19.  
     
  9. asdx

    asdx

    Joined:
    Jun 3, 2013
    Posts:
    18
    It seems that my stupid mistake was that I did not tagged my Main Camera. I previously deleted it, and was naming, not tagging it. I realized it just before reading the posts so I believe KelsoMRK's script would of spot it easily. Ty so much guys! I was in a real pickle :)