- Home /
How can I fix this screen wrapping?
So I am currently trying to get a ship to wrap around the screen when the player moves out of bounds (like in Asteroids) and I've been hopping around tutorials trying to find the best way to do this. I eventually came up with this code for the x axis, and it kind of works
What I mean by that is that when the player goes off the left or right side of the screen, they pop up on the other side... but they are unable to move along the x axis. However, they can still move up and down along the y axis.
I am sure it is something wrong with my code, such as a logic error or something, but I can't figure out what it is. It kind of seems like once they move off screen one time they get stuck in one of those if statements and keep having their X position set to left side + buffer or right side - buffer.
I am also aware that I should set the x positions to left side - buffer and right side + buffer to achieve the desired effect of having a smooth wrap, I am just using that right now to test things out.
Any help is appreciated, thanks! :)
// player ship game object
public GameObject player;
// player position tracker
Vector3 position;
// tracks right and left camera sides
float leftSide = 0.0f;
float rightSide = 0.0f;
//value that makes it so ship disappears offscreen before reappearion on other side
float buffer = 1.0f;
float distanceZ = 10.0f;
// Use this for initialization
void Start ()
{
// uses a specific distance
leftSide = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, distanceZ)).x;
rightSide = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, distanceZ)).x;
}
// Update is called once per frame
void Update ()
{
position = player.transform.position;
if (position.x < leftSide - buffer)
{
position.x = rightSide - buffer;
player.transform.position = position;
}
if (position.x > rightSide + buffer)
{
position.x = leftSide + buffer;
player.transform.position = position;
}
// set new position
player.transform.position = position;
position = player.transform.position;
}
Answer by sharnold52 · Sep 29, 2017 at 11:24 PM
Turns out this script was right. It was just clashing with another one of my scripts in the game.
Your answer
Follow this Question
Related Questions
2d camera script, strange glitches appears 0 Answers
2D Orthographic Main Camera and UI 0 Answers
Camera setup question 2 Answers
Cinemachine weird lines of color when moving? 1 Answer
How do I get rid of this motion blur? 0 Answers