# Changing coordinates of the object with velocity preservation

In Doodle jump, if a player jumps over one side of the screen he appears on another side with preserved velocity and other physical parameters.

I made two trigger colliders on the sides of the screen with OnTriggerEnter2D method.

private void OnTriggerEnter2D(Collider2D collision)
{
collision.transform.position = new Vector3(collision.transform.position.x * -0.95f,
collision.transform.position.y, collision.transform.position.z);
}


If I multiply collision.transform.position.x by -1f then on collision some glitching occurs. It gets stuck for a second. I believe this happens because when player is moved to the other side there is another collision detected and he is thrown back. Is there a more elegant way of handling this instead of multiplying by 0.95f?

Final solution:

[Tooltip("Controls where is the edge of the level where player is trasfered on the opposite side of the level.")]
[SerializeField]
private float xAxisMovementConstraints;

private void DetectBoundryCross()
{
if (transform.position.x < (-xAxisMovementConstraints))
{
transform.position = new Vector2(transform.position.x + (xAxisMovementConstraints * 2f),
transform.position.y);
}
else if (transform.position.x > xAxisMovementConstraints)
{
transform.position = new Vector2(transform.position.x - xAxisMovementConstraints * 2f,
transform.position.y);
}
}

• Position and velocity are generally separate (unless you've written your own Verlet integrator instead of the default Box2D one...), so this should "just work(TM)". What have you tried, and what's not working the way you want it to so far? Apr 7 '18 at 9:27
• I made 2 colliders on one empty object. Both on the sides of the screen. Script essentially: private void OnTriggerEnter2D(ref Collider2D collision) { collision.transform.position = new Vector3(collision.transform.position.x * -1f, collision.transform.position.y, collision.transform.position.z); } When I enter one of the colliders I have a crash. Apr 7 '18 at 10:27
• I removed one collider and extended another on entire screen. Change method to OnTriggerExit2D. Same. Apr 7 '18 at 10:30
• Wow. Actually I removed "ref" and it works now! Don't know why I put it there in a first place. Apr 7 '18 at 10:31
• But it kinda glithcy because it has a hard time to decide where to set a player because when it sets player on another side I have another collision and it throws me back. Apr 7 '18 at 10:40

I haven't tested this, but my approach would be to not use colliders for this at all.

A simple script should be all you need.

using UnityEngine;
using System.Collections;

public class SideWrap : MonoBehavior {
public float left_boundary = -5.0f;
public float right_boundary = 5.0f;

void Update()
{
float newX = transform.position.x;
if(transform.position.x < left_boundary){
newX = transform.position.x + (right_boundary - left_boundary);
transform.position = new Vector2( newX, transform.position.y );
}
else if(transform.position.x > right_boundary){
newX = transform.position.x - left_boundary;
transform.position = new Vector2( newX, transform.position.y );
}
}
}


Attaching this to the player object should give your desired result.

• transform.position.x can't be modified but I got a point. private void 'DetectBoundryCross() { if (transform.position.x < (-widthMovementConstraints)) { transform.position = new Vector2(transform.position.x + (widthMovementConstraints + widthMovementConstraints), transform.position.y); } else if (transform.position.x > widthMovementConstraints) { transform.position = new Vector2(transform.position.x - widthMovementConstraints * 2f, transform.position.y); } }' Apr 11 '18 at 8:14
• Ah yes, I'll correct the syntax when I get to a computer. Glad to help. Apr 11 '18 at 12:08