- Home /
Melee for 2.5D Platformer? (C#)
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<Player>();
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<Player> ();
if (collision.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<Player> ().PlayerHit ();
}
if (collision.gameObject.tag == "AttackBox")
{
pstats.CurrentEXP += 25;
EnemyDeath ();
}
}
void OnTriggerStay(Collider collision)
{
Player pstats = Player.GetComponent<Player> ();
if (collision.gameObject.tag == "AttackBox")
{
pstats.CurrentEXP += 25;
EnemyDeath ();
}
}
void OnTriggerExit(Collider collision)
{
Player pstats = Player.GetComponent<Player> ();
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.
If you don't need specific target area maybe you could work with the distance between your player and the enemy ?
float distance = Vector3.Distance(transform.position, enemy.transform.position);
for each frame the attack is on if the distance is lower than 1 or 2 units you are at melee distance and then just solve whatever you're trying to do ?
Well, that was the alternative idea I wrote in my question. Though I'd specifically need the distance on the X- and Y-Axis, Also, how do I get to my enemy.transform.position? don't I need to specify that as a gameobject "enemy"? If I do so, do I have to give each enemy a different tag or will something like "gameobject.findgameobjectwithtag("enemy")" just take the closest one? Or do I just do the calculation for each enemy at every run through update or the attack method?
Your answer
Follow this Question
Related Questions
change tiltshift distance with mousewheel 0 Answers
left and right 2d walk 1 Answer
2D Movement Problems 2 Answers
Player freezes upon attaching a "connected body" to a hinge/distance joint 0 Answers