Search Unity

How do make a player leave the play area and have them re-enter from the opposite side

Discussion in 'Scripting' started by arabellemagdamit, Oct 8, 2018.

  1. arabellemagdamit

    arabellemagdamit

    Joined:
    Sep 4, 2018
    Posts:
    4
    Hello Everyone!
    I'm doing this for my Midterm Practical and I need help quick please. I am a beginner at Unity.
    My entire goal here is to make my own game with all the notes and help that I can get.

    So here is the goal in this thread: Using "if" checks, limit the players movement between -5 and 5 in the "x" axis and in the "y" axis. If they leave this area, have them re-enter from the opposite side.

    I believe this method is called Screen Wrapping. I've tried most of the tutorials on YouTube and somehow I just don't get it.

    Here is my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 10f;
    8.     public float turnSpeed = 50f;
    9.     Vector3 movement;
    10.     Rigidbody playerRigidbody;
    11.     public float xMinBound, xMaxBound, yMinBound, yMaxBound;
    12.     private Vector3 direction;
    13.  
    14.     void Start ()
    15.     {
    16.         playerRigidbody = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.    
    20.  
    21.     private void Update()
    22.     {
    23.        
    24.  
    25.         //Move(Vector3 down);
    26.         Move(direction);
    27.         bool isOffScreen = OffScreen(yMinBound);
    28.  
    29.         if (isOffScreen)
    30.         {
    31.             this.transform.position = Vector3.zero;
    32.         }
    33.  
    34.         direction = BoundsCheck(xMinBound, xMaxBound, yMaxBound);
    35.  
    36.      
    37.  
    38.         if (Input.GetKey(KeyCode.W))
    39.         {
    40.             transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    41.         }
    42.  
    43.         if (Input.GetKey(KeyCode.S))
    44.         {
    45.             transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
    46.         }
    47.  
    48.         if (Input.GetKey(KeyCode.A))
    49.         {
    50.             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
    51.         }
    52.         if (Input.GetKey(KeyCode.D))
    53.         {
    54.             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    55.         }
    56.         if (Input.GetKey(KeyCode.Q))
    57.         {
    58.             transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
    59.         }
    60.         if (Input.GetKey(KeyCode.E))
    61.         {
    62.             transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
    63.  
    64.         }
    65.  
    66.  
    67.     }
    68.  
    69.     private void OnCollisionEnter(Collision collision)
    70.     {
    71.         Destroy(this.gameObject);
    72.     }
    73.  
    74.      
    75.  
    76.  
    77.     private void Move(Vector3 dir)
    78.     {
    79.         this.transform.Translate(dir * Time.deltaTime * 10);
    80.     }
    81.     private bool OffScreen(float yMinBound)
    82.     {
    83.         if (this.transform.position.y < yMinBound)
    84.         {
    85.          
    86.             direction = new Vector3(Random.Range(-5, 5), 1, 0);
    87.             direction.Normalize();
    88.             return true;
    89.         }
    90.         return false;
    91.     }
    92.  
    93.     private Vector3 BoundsCheck(float xMin, float xMax, float yMax)
    94.     {
    95.         Vector3 dir = this.direction;
    96.  
    97.         if (this.transform.position.x <= xMin || this.transform.position.x >= xMax)
    98.         {
    99.             dir.x = -dir.x;
    100.         }
    101.         else if (this.transform.position.y >= yMax)
    102.         {
    103.             dir.y = -dir.y;
    104.         }
    105.         return dir;
    106.     }
    107.  
    108.    
    109. }
    110.  
    111.  
    112.  
    113.  
    114.  
    115.    
    116.  
    (Ignore the other codes, they are for other components and they work well! Except the screen wrapping)
    As you can tell, I've attempted to do screen wrapping but it doesn't do anything when I play the game.

    Here is a screen shot:
    Screenshot (110).png

    That helicopter outside the play area is the player, it shouldn't be going out like that!
    The border that I should have is a 5x5 (rn I have a 15 x 15 play area) and when the player exits that 5x5, should re-enter the opposite side.
    I need help with my script.

    Thank you so much in advance!
     

    Attached Files:

  2. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    I assume the center is the origin (coords 0,0,0) so this wont work otherwise:
    Code (CSharp):
    1.  
    2.     public float xMaxDisplacement, zMaxDisplacement;
    3.  
    4.     private void Update() {
    5.  
    6.          ///Constraining the player to a small area
    7.          //If the player goes too far to the right, it is teleported to the left
    8.          if (transform.position.x > xMaxDisplacement) { transform.position = new Vector3(-xMaxDisplacement, transform.position.y, transform.position.z) }
    9.          //If the player goes too far to the left, it is teleported to the right
    10.          if (transform.position.x < -xMaxDisplacement) { transform.position = new Vector3(xMaxDisplacement, transform.position.y, transform.position.z) }
    11.          //If the player goes too far to the... you get how this works already.
    12.          if (transform.position.z > zMaxDisplacement) { transform.position = new Vector3(transform.position.x, transform.position.y, -zMaxDisplacement) }
    13.          if (transform.position.z < -zMaxDisplacement) { transform.position = new Vector3(transform.position.z, transform.position.y, zMaxDisplacement) }
    14.  
    15.     }
    16.  
    17.  
    Note however, that the transition will look abrupt. You might want to put some kind of portal-looking thing to make it look smoother?
    Its also worth mentioning that the velocity will be conserved.
     
    Last edited: Oct 9, 2018
    Ryiah likes this.