How to enable vertical movement in certain areas only 2d
I'm making a 2d game. The whole map, including the ladders, is part of one sprite. I want to enable vertical movement where the ladders are only. How do I do this?
Answer by Ninjapro2k_ · Dec 02, 2016 at 08:16 PM
I'm guessing you want something like Donkey Kong, right?
Try this: public class ClimbLadder : MonoBehaviour {
public Transform ladder1;
public Transform ladder2;
//etc...
public bool atLadder1 = false;
public bool atLadder2 = false;
//etc...
float laddercheckRadius = (somewhere around 3f);
void Update () {
atLadder1 = Physics2D.OverlapArea ([Parameters][2]);
atLadder2 = Physics2D.OverlapArea (Parameters);
//etc...
if (Input.GetButtonDown ("your climb button") && atLadder1)
//Whatever function you want to use to climb
}
}
It should work something like this, sorry I wasn't able to put full focus into it. ladder1, 2 and so on will be empty gameObjects placed on the ladders. This code goes on the player, and needs some Collider2D on it. Hmu if it doesn't work, I'll try to test it for myself.
[2]: https://docs.unity3d.com/ScriptReference/Physics2D.OverlapArea.html
Your answer
Follow this Question
Related Questions
How to move a 2D object toward the position it is pointing to? 0 Answers
2D animation transitions and unexpected position changes 2 Answers
Sprite doesn't render 0 Answers
Adding sprites to same layer as bone in 2D animation rig 1 Answer
2D Outline Shader working in Node Preview But not when rendering 0 Answers