Quantcast
Channel: Questions in topic: "melee"
Viewing all articles
Browse latest Browse all 201

Melee for 2.5D Platformer? (C#)

$
0
0
I'm creating (as the title suggests) a 2.5D Platformer and wanted to add the option for the player to attack enemies in melee. I am having some problems getting it to work though since I'm rather new to Unity. My current attempt was to create a box collider that floats infront of my character and gets only set to "enabled" for the duration of one attack-swing. That part seems to work (from what I can tell by my Debug.Log entries. Though the hit is never regitered by the enemy. My current code for the box-collider looks like this: public GameObject Player; bool attack = false; void Awake () { Player = GameObject.FindGameObjectWithTag ("Player"); gameObject.collider.enabled = false; } void Update () { Player pstats = Player.GetComponent(); attack = pstats.attacking; if (!attack) { gameObject.collider.enabled = false; } if (attack) { gameObject.collider.enabled = true; Debug.Log ("Box active"); } } The Enemyscript contains this: void OnTriggerEnter(Collider collision) { Player pstats = Player.GetComponent (); if (collision.gameObject.tag == "Player") { collision.gameObject.GetComponent ().PlayerHit (); } if (collision.gameObject.tag == "AttackBox") { pstats.CurrentEXP += 25; EnemyDeath (); } } void OnTriggerStay(Collider collision) { Player pstats = Player.GetComponent (); if (collision.gameObject.tag == "AttackBox") { pstats.CurrentEXP += 25; EnemyDeath (); } } void OnTriggerExit(Collider collision) { Player pstats = Player.GetComponent (); if (collision.gameObject.tag == "AttackBox") { pstats.CurrentEXP += 25; EnemyDeath (); } } I tried different Trigger methods there, none seems to work. A different idea I had was to drop the box collider and use the distance between player and enemy instead. Something like a test of sorts (pseudocode) if (distance player to enemy on x-axis < X-Range && distance on y-axis < Y-Range) Enemy gets hit To me that even seems an easier solution, though I don't know how to implement it properly that I can messure the distance on one axis. Also I'd have to check if the player is infront or behind the enemy (I have two methods SlashRight and SlashLeft). Any help would be highly appreciated, for either of the two approaches.

Viewing all articles
Browse latest Browse all 201

Trending Articles



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