- Home /
How would i go about implementing a melee system in a 2.5d game
So what i have in this 2.5d game is a simple movement and jump script and i would like to try and implement a melee script that can attack both left and right smoothly and make it so that it does damage but i don't know where to start.
This is my script:
public Transform playertrn;
public Rigidbody playerRB;
public int speed;
private bool isFalling = false;
private bool doubleJump = false;
public Vector3 JumpHeight = new Vector3 (0, 8, 0);
void Start ()
{
playertrn = GetComponent<Transform> ();
playerRB = GetComponent<Rigidbody> ();
Physics.gravity = new Vector3(0, -11.0F, 0);
}
void Update ()
{
if (Input.GetKey (KeyCode.D))
{
playertrn.Translate (new Vector3(speed, 0, 0) * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A))
{
playertrn.Translate (new Vector3 (-speed, 0, 0) * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.Space) && !isFalling)
{
playerRB.velocity = JumpHeight;
isFalling = true;
}
} void OnCollisionStay () { isFalling = false; } }
would i be able to get some advice on where to start with the melee system please
Thanks in advance
I use a SphereCastAll to get all enemy colliders in the character's vincinity, then compare each enemy transform.position against the forward direction of the attacker so I only get those enemies in a narrow arc in front (for a slashing attack). If only exactly one enemy should be able to get hit, I just take the one with the smallest angle that is still withing the attacking angle threshold.
Answer by CheetahSpeedLion · Jul 27, 2017 at 03:12 AM
The way I do my melee systems is by spawning a box collider, or box collider 2D in your case, where the attack is supposed to be happening. The enemy then has a hitbox that registers OnTriggerEnter() and lowers the enemy's health, knockback, etc. The "attack box", let's call it, can carry any info you'd like such as amount of damage and who sent the attack, which can then be transmitted to the enemy using OnTriggerEnter().
There are other ways to do it but this is my personal favorite. Another way is using raycasts I think.
Best of luck, Areleli Games
Thank you so much i will look into this method the only issue i see is making it so that collider will spawn on either side depending on which way the player is facing
good answer by cheetah. another option would be to create a seperate collider or trigger on the edge of a sword. if you run into problems getting a seperate collision. just make a seperate rigidboby and collider on an unparented empty gameobject and update its location to the tip of the sword everyframe
Ok thank you that makes a lot more sense. you guys have helped me heaps thank you
Your answer
Follow this Question
Related Questions
Weapon attack 0 Answers
Rotate scene or camera with unity 2d physics 0 Answers
How to achieve 2.5D movement along curve? 1 Answer