Search Unity

Best method for detecting when a player is out of screens view.

Discussion in 'Scripting' started by CoffeeConundrum, Jan 1, 2015.

  1. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    I'm creating a game where I would like it where if the player goes off on one side of the screen, they reappear on the opposite side.

    I'm curious as how to set this up in Unity and what the best practise would be.

    Many thanks
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    you can just change the position.

    for example:

    if position on the x is > 10
    position on the x = -10

    That says if you go to the right, you appear at the left.
     
  3. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    player position vs screen width and height
    http://docs.unity3d.com/ScriptReference/Screen.html


    basic example:
    Code (CSharp):
    1. //somewhere in Update or how ever you deal with movement
    2.  
    3. if(player.transform.position.x > Screen.width){
    4.    // we are past the screen edge... reset to 0 (other side)
    5.    player.transform.position = new Vector3(0, player.transform.position.y,  player.transform.position.z);
    6. }
     
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    The OnBecameInvisible event is the best way that I know. If not the best then it's definitely the simplest.
     
    novashot likes this.
  5. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    I was going to suggest this but he also said they disappear off one side of the screen to reappear on the other.