Premiers pas avec Unity2D 5 Monter l'échelle

Getting Started With Unity2d 5 Climbing Ladder



Code complet

public float climbspeed = 2f private bool inLadder public Collider2D platf private void OnTriggerStay2D(Collider2D collision) { if (collision.tag == 'Player') { if (Input.GetKey(KeyCode.UpArrow)) { collision.SendMessage('setLadder', true) collision.SendMessage('setInLadder', false) inLadder = true collision.GetComponent<Rigidbody2D>().gravityScale = 0 Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[0], platf) Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[1], platf) collision.GetComponent<Rigidbody2D>().velocity = new Vector2(0, climbspeed) } else if (Input.GetKey(KeyCode.DownArrow)) { collision.SendMessage('setLadder', true) collision.SendMessage('setInLadder', false) inLadder = true collision.GetComponent<Rigidbody2D>().gravityScale = 0 Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[0], platf) Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[1], platf) collision.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -climbspeed) } else if (Input.GetKey(KeyCode.Space)) { collision.SendMessage('setInLadder', false) collision.SendMessage('setLadder', false) Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[1], platf, false) Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[0], platf, false) collision.GetComponent<Rigidbody2D>().gravityScale = 1 inLadder = false } else if (inLadder) { collision.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0) collision.SendMessage('setLadder', false) collision.SendMessage('setInLadder', true) } } } private void OnTriggerExit2D(Collider2D collision) { collision.GetComponent<Rigidbody2D>().gravityScale = 1 collision.SendMessage('setLadder', false) collision.SendMessage('setInLadder', false) inLadder = false Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[1], platf, false) Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[0], platf, false) }

Prenons l'escalade comme exemple

if (Input.GetKey(KeyCode.UpArrow)) { collision.SendMessage('setLadder', true) collision.SendMessage('setInLadder', false) inLadder = true collision.GetComponent<Rigidbody2D>().gravityScale = 0 Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[0], platf) Physics2D.IgnoreCollision(collision.GetComponents<Collider2D>()[1], platf) collision.GetComponent<Rigidbody2D>().velocity = new Vector2(0, climbspeed) }

Utilisez deux variables booléennes «setLadder» et «setInLadder» pour contrôler les deux animations sur l'échelle et l'escalade.

Ignorer la collision entre le joueur et la plate-forme sur l'échelle
et sur l'échelle, le poids du corps rigide est égal à 0 et la rampe a une vitesse de montée à vitesse fixe.