0
\$\begingroup\$

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.

enter image description here

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?

enter image description here


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);
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Apr 7, 2018 at 9:27
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 7, 2018 at 10:27
  • \$\begingroup\$ I removed one collider and extended another on entire screen. Change method to OnTriggerExit2D. Same. \$\endgroup\$ Apr 7, 2018 at 10:30
  • \$\begingroup\$ Wow. Actually I removed "ref" and it works now! Don't know why I put it there in a first place. \$\endgroup\$ Apr 7, 2018 at 10:31
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 7, 2018 at 10:40

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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); } }' \$\endgroup\$ Apr 11, 2018 at 8:14
  • \$\begingroup\$ Ah yes, I'll correct the syntax when I get to a computer. Glad to help. \$\endgroup\$
    – Stephan
    Apr 11, 2018 at 12:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .