Multiple If Statements Won't Work Together - Relocating Objects / Screen Wrap

HI

I’m trying to create a screen wrap effect which should be pretty simple. Its a Top down game.

I put the If Statements in its own function because I read that fixes my problem (but it doesn’t).

This is my code. (ScreenWrap is being called in Update)

    public float xMax = 40;
	public float xMin = -40;
	public float zMax = 30;
	public float zMin = -30;


void ScreenWrap()
	{

		if (transform.position.x <= xMin) 
		{
			transform.position = new Vector3(-transform.position.x, transform.position.y, transform.position.z);		
		}
		
		if (transform.position.x >= xMax) 
		{
			transform.position = new Vector3(-transform.position.x, transform.position.y, transform.position.z);		
		}

		if (transform.position.z <= zMin) 
		{
			transform.position = new Vector3(transform.position.x, transform.position.y, -transform.position.z);		
		}

		if (transform.position.z >= zMax) 
		{
			transform.position = new Vector3(transform.position.x, transform.position.y, -transform.position.z);		
		}
	}

This is the only one that works and it doesn’t matter which order its in.

 if (transform.position.x <= xMin) 
        {
            transform.position = new Vector3(-transform.position.x, transform.position.y, transform.position.z);        
        }

I started out with >= in all the if statement then it went to == which then it went to how it is now. Cant understand why only one works. No doubt I’ve done this with a clearly obvious mistake. Can anyone help please. :slight_smile:

Lets say your x value goes to 41. Its more tham max so You make it negative. It becomes -41 which is less than min so you negate it again … it never gets solved in the worst case.

Also, because you are using separate if’s (no else), the latter condition will always become true if the first one does. This might make tracing the problem confusing.

I’ll try to fix your code here but im on mobile so ill probably jus mess it up :slight_smile:
Shoul be something like…

        float outBy;
        if (transform.position.x < xMin) 
        {
            // if x==-41, outBy will be -1
            outBy = transform.position.x - xMin;   
            // new x becomes 39
            transform.position = new Vector3(xMax + outBy, transform.position.y, transform.position.z);     
            // we could just set x to xMax but that would mean 
            // the object loses speed and might cause the wrap to appear jerky 


        }
        else if (transform.position.x > xMax) 
        {
            // when you enter the function there's no way x can be both 
            // less than min and more than max at the same time. 
            // therefore use "else" -> only check xMax if x was more than  xMin 
            outBy = transform.position.x - xMax;
            transform.position = new Vector3(xMin + outBy, transform.position.y, transform.position.z);        
        }
    }

So measure how much the object is out of screen and move it in from the opposite (min/max) by that much