Search Unity

WorldToScreenPoint on multiple screens (split screen)

Discussion in 'Scripting' started by raycosantana, Dec 17, 2014.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Hey I have this split screen game where the player have the life bar and player name over their player character, how can I use WorldToScreenPoint with multiple cameras? its seems the life bar only work on the first player camera and the other player see ghost life bars floating around lol as if the first camera occupied the whole screen.
     
  2. Hyper-Luminal-Games

    Hyper-Luminal-Games

    Joined:
    Jun 24, 2014
    Posts:
    108
    raycosantana likes this.
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Last edited: Dec 18, 2014
  4. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    can anyone explain a little bit more how to use this, I have been trying for hours but the GUI keeps drawing outside the camera.
    this is an extract of the code I have right now:


    Code (CSharp):
    1. Vector3 ViewPortPos = Camera.main.WorldToViewportPoint(PlayerCharacter.transform.position);
    2.         Vector3 screenPos = Camera.main.ViewportToScreenPoint(ViewPortPos);
    3.         x = screenPos.x - xOffset; // x offset
    4.         y = screenPos.y - yOffset; // y offset
    5.  
    6. .....
    7.  
    8.    void OnGUI() {
    9.         GUI.DrawTexture(new Rect(x, y, 32, 4), xGraphicsManager.Instance.LifeBarBackGround);
    10.         GUI.DrawTexture(new Rect(x, y, 32 * CurHealth / Health, 4), xGraphicsManager.Instance.LifeBar);
    11.         GUI.DrawTexture(new Rect(x + 8, y - 16, 16, 32), PlayerIcon);
    12.  
    13.     }
    at first I figured out the viewports works with normalized values so I thought I could just multiply those values by the Camera width and height but the problem is, if something is, lets say for example (1.5, 0.5, 32) it is still draw on screen even if 1.5 is outside the viewport because 1.5*Camera width is still a valid pixel rect inside the screen... :confused::confused:
     
  5. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Ok, in case someone has the same problem as me this is what I did:

    Code (CSharp):
    1. private Camera[] Cameras;
    2. void Start () {
    3.         Cameras = Camera.allCameras;
    4.     }
    5. void OnGUI() {
    6.         foreach (Camera Cam in Cameras){
    7.         Vector3 ViewPortPos = Cam.WorldToViewportPoint(PlayerCharacter.transform.position);
    8.         if (ViewPortPos.x > 0.05f && ViewPortPos.x < 0.95f && ViewPortPos.y > 0.05f && ViewPortPos.y < 0.95f){
    9.         Vector3 screenPos =  Cam.ViewportToScreenPoint(ViewPortPos);
    10.         x = screenPos.x - xOffset; // x offset
    11.         y = Screen.height - screenPos.y - yOffset; // y offset
    12.         GUI.DrawTexture(new Rect(x, y, 32, 4), xGraphicsManager.Instance.LifeBarBackGround);
    13.         GUI.DrawTexture(new Rect(x, y, 32 * CurHealth / Health, 4), xGraphicsManager.Instance.LifeBar);
    14.         GUI.DrawTexture(new Rect(x + 8, y - 16, 16, 32), PlayerIcon);
    15.             }
    16.         }
    17.  
    18.     }