DoodleJump type gameplay part 2, side teleporting, Unity 3D

by on under Code snippets
2 minute read

EDIT: Better way of doing this is on youtube. (To make it work for any size/resolution you will need to move the cameras)

In the last part I showed how to jump through platforms. Now I will show you how to implement the side teleporting. (example)
Demo This code is attached on the player:

void OnTriggerEnter2D(Collider2D other){
	if(other.gameObject.name == "RightColl" || other.gameObject.name == "LeftColl"){
		transform.position = new Vector2(transform.position.x / Mathf.Abs (transform.position.x) - transform.position.x, transform.position.y);
	}
}

You need to make 2 empty gameObjects, with the names LeftColl and RightColl, and attach a 2D box collider to them. On the 2D box collider check isTrigger.

public Camera mainCam;
public BoxCollider2D leftColl;
public BoxCollider2D rightColl;
void Start(){
    rightColl.size = new Vector2(3f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
    rightColl.center = new Vector2(mainCam.ScreenToWorldPoint (new Vector3(Screen.width, 0f, 0f)).x + 3f, rightColl.size.y/2);
    leftColl.size = new Vector2(3f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
    leftColl.center = new Vector2(mainCam.ScreenToWorldPoint (new Vector3(0f, 0f, 0f)).x - 3f, leftColl.size.y/2);
}

This piece of code will set the colliders to the right coords.

Unity 3D, code snippet, doodle jump, platform, side teleport
comments powered by Disqus