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);
}
}