Search Unity

WorldToScreenPoint

Discussion in 'iOS and tvOS' started by robhuhn, Jul 24, 2010.

  1. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    hi i just figured out some differences between
    Code (csharp):
    1. Camera.mainCamera
    and
    Code (csharp):
    1. GameObject.Find("Main Camera").camera
    Both commands return the same camera object.


    So what i want to do is calculate the position of an world object outside the screen bounds to indicate an arrow on the screen. The arrow points to that world object.

    Now i have two cameras in my scene. One main camera and one gui camera. For my calculations i have to find the main camera.

    When using Camera.mainCamera i get the gui camera so i saved my main camera into a field:
    Code (csharp):
    1. public void Start()
    2. {
    3.         mainCamera = GameObject.Find("Main Camera").camera;
    4. }
    The point is that the calculation with the field mainCamera works different from using Camera.mainCamera when switching off the gui cam. Although mainCamera and Camera.mainCamera return the same camera object.


    Does anyone know about that issue? Is it a bug or is it purposed?
     

    Attached Files:

  2. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    here are some further code snippets for a better understanding:

    Code (csharp):
    1. if (!IsInScreenBounds(transform.position)  state == ScreenState.inScreen)
    2. {
    3.             state = ScreenState.outOfScreen;
    4.             directionArrow = (GameObject)Instantiate(directionArrowPrefab, Vector3.zero, Quaternion.identity);
    5.             MessageRouter.Instance.ShowDirectionArrow(gameObject, directionArrow
    6. }

    listener:
    Code (csharp):
    1. private void OnShowDirectionArrow(GameObject source, GameObject arrow)
    2. {
    3.         arrow.transform.parent = transform;
    4.         DirectionArrow directionArrowComponent = (DirectionArrow)arrow.GetComponent(typeof(DirectionArrow));
    5.         directionArrowComponent.target = source;
    6.  
    7. }

    the calculation of the arrows position:
    Code (csharp):
    1. private void UpdatePosition()
    2. {
    3.         targetScreenPosition = Camera.mainCamera.WorldToScreenPoint(target.transform.position);
    4.         targetScreenPositionFromCenter = screenCenter - targetScreenPosition;
    5.  
    6. ...
    7.  
    8.  
     
  3. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    i found the issue. there is no bug it was just me. There was a second Camera.mainCamera access :cry: