Quantcast
Channel: Questions in topic: "tagging"
Viewing all articles
Browse latest Browse all 120

2D Platformer: Failure to jump through platform

$
0
0
Hi! I'm working on a 2D platformer right now in Unity 2021.1.17f1, and I would like the player to be able to jump through specific (tagged) platforms when they press the down key. Unfortunately, nothing is exactly working, and I've tried quite a bit (see: [1][1], [2][2], [3][3], [4][4], [5][5], [6][6]). This is something like day six of the same headache.

The code below is the most "normal" gameplay possible, but it also isn't perfect. The player can clip through the floor and then fall off the map. Trying to combine the Drop Platform tag with pressing the Down key never fired correctly: the Debug.Log statements would only return about 30% of the time. I am not sure why.

The player has a Capsule collider. The platforms all have box colliders and platform effectors that allow the player to jump up through the bottom and then walk on top of them, which I would also like to keep! I just can't figure out how to let the player jump down. I have tried using triggers. It did not work.

Any advice possible would be greatly appreciated. ---------- public float waiting = 0.5f; public bool droppable = false; void Update() { if (Input.GetKeyDown(KeyCode.DownArrow)) { StartCoroutine(FallTimer()); } if (Input.GetKeyUp(KeyCode.DownArrow)) { GetComponent().enabled = true; } } IEnumerator FallTimer() { GetComponent().enabled = false; GetComponent().gravityScale += 4; //falls faster yield return new WaitForSeconds(waiting); //waits a variable amount of time before re-enabling capsule collider GetComponent().enabled = true; GetComponent().gravityScale -= 4; } void OnTriggerStay2D(Collider2D other) { if (other.gameObject.tag == "Drop Platform") { //GetComponent().isTrigger = true; (the simplest and messiest solution, hence why it's commented out) droppable = true; } } void OnTriggerExit2D(Collider2D other) { if (other.gameObject.tag == "Drop Platform") { //GetComponent().isTrigger = false; droppable = false; } } [1]: https://answers.unity.com/questions/637336/tagging-a-collider.html [2]: https://www.reddit.com/r/Unity2D/comments/5ct3m9/help_with_how_to_make_a_platform_you_can_drop/ [3]: https://levelup.gitconnected.com/using-the-tag-system-in-unity-and-damaging-the-player-f8267c1c7420 [4]: https://www.reddit.com/r/Unity2D/comments/fegjx9/how_do_i_make_one_way_platforms_that_only_the/ [5]: https://forum.unity.com/threads/jumping-down-through-a-platform.414902/ [6]: https://gamedev.stackexchange.com/questions/189021/how-can-i-jump-down-from-a-platform-in-unity

Viewing all articles
Browse latest Browse all 120

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>