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

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:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 10f;
public float turnSpeed = 50f;
Vector3 movement;
Rigidbody playerRigidbody;
public float xMinBound, xMaxBound, yMinBound, yMaxBound;
private Vector3 direction;

void Start ()
{
playerRigidbody = GetComponent<Rigidbody>();
}

private void Update()
{

//Move(Vector3 down);
Move(direction);
bool isOffScreen = OffScreen(yMinBound);

if (isOffScreen)
{
this.transform.position = Vector3.zero;
}

direction = BoundsCheck(xMinBound, xMaxBound, yMaxBound);

if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

if (Input.GetKey(KeyCode.S))
{
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
}

if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Q))
{
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E))
{
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);

}

}

private void OnCollisionEnter(Collision collision)
{
Destroy(this.gameObject);
}

private void Move(Vector3 dir)
{
this.transform.Translate(dir * Time.deltaTime * 10);
}
private bool OffScreen(float yMinBound)
{
if (this.transform.position.y < yMinBound)
{

direction = new Vector3(Random.Range(-5, 5), 1, 0);
direction.Normalize();
return true;
}
return false;
}

private Vector3 BoundsCheck(float xMin, float xMax, float yMax)
{
Vector3 dir = this.direction;

if (this.transform.position.x <= xMin || this.transform.position.x >= xMax)
{
dir.x = -dir.x;
}
else if (this.transform.position.y >= yMax)
{
dir.y = -dir.y;
}
return dir;
}

}
``````

(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:

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!

I assume the center is the origin (coords 0,0,0) so this wont work otherwise:

``````    public float xMaxDisplacement, zMaxDisplacement;

private void Update() {

///Constraining the player to a small area
//If the player goes too far to the right, it is teleported to the left
if (transform.position.x > xMaxDisplacement) { transform.position = new Vector3(-xMaxDisplacement, transform.position.y, transform.position.z) }
//If the player goes too far to the left, it is teleported to the right
if (transform.position.x < -xMaxDisplacement) { transform.position = new Vector3(xMaxDisplacement, transform.position.y, transform.position.z) }
//If the player goes too far to the... you get how this works already.
if (transform.position.z > zMaxDisplacement) { transform.position = new Vector3(transform.position.x, transform.position.y, -zMaxDisplacement) }
if (transform.position.z < -zMaxDisplacement) { transform.position = new Vector3(transform.position.z, transform.position.y, zMaxDisplacement) }

}
``````

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.

1 Like