- Home /
2D sidescroller combat, how do I start?
I want my player to have two methods of attack, a melee sword attack, but also a jumping attack (think mario). The melee sword attack I have a working script for, but I'm unsure how to start the jumping attack.
I want the player to be damaged if they walk sideways into the enemy, but if they jump and land on the enemy's head, the enemy dies. Should I create a trigger zone on top of the enemy's head to trigger the ReceiveDamage() script? Or should I programatically check if the player's transform is right above the enemy's?
I'm working in C#, but pseudocode is all I really need! Thanks ahead of time.
What kind of objects is your character and your enemies? Character controllers? Rigidbodies?
@robertbu $$anonymous$$y Player has a Character Controller. I currently have my enemies as Rigidbodies but may change that if it becomes necessary
Answer by Cerbion_ · Jul 12, 2013 at 06:15 PM
In the original Mario series (where you referred to, and that's basically the core idea for what you have). It's actually made that in the following way:
While the Player is moving downwards (e.g. after a jump),
Check if the Player if the playercollides with an enemy,
If so > Kill enemy
I hope that answers the question. :)
EDIT: That only works if all the enemies do not fall faster than the player (which actually shouldn't be the case normally, but you might play around with gravity here and then, so keep that in mind.)
im not sure how that would work, since u would still be jumping and landing right into them from the side , when u collide if your feet have not reached the bottom yet, you are now beside the enemy
markedagain has a good point. If the player touches the side of the enemy at any point, in air or not, they will be damaged. Perhaps there's a way to check if the collision is with only the top of a box collider and not the sides?
for that he would need 3 box colliders, to make sure the top one does not get hit from the side , so like a upside down U shape of collision box's
I think I would need 4 box colliders, just to cover for the rare situation that the enemy falls/jumps on top of the player. 4 colliders is a bit tedious but not hard. Are there any downsides to doing this? Will 4 colliders per enemy affect game performance if there are ~3 enemies on screen at any time?
You could work around it and check if the player (who collides with the enemy) is above the Enemy (if the Player position Y is > than the Enemy Position Y or something, so you don't have to make even a second collider, on the other hand, if you want to make it rather object oriented, go with only 1 extra box collider on the very top, which would mark the "weak spot" of the enemy. Combine that with 'robertbu's idea of using Raycasts and nothing should go wrong and you don't use unnecessary Gameobjects.
As for you worries: yes, a lot gameobjects with colliders will impact your performance, but only briefly if you don't spawn 100+ enemies at once, however it raises the risk of any glitches to happen: too many colliders near each other and in movement = glitchez inco$$anonymous$$g.
So keep that in $$anonymous$$d.
Answer by markedagain · Jul 12, 2013 at 06:17 PM
i would prob stick with the 1 collider on the enmey and on the OnCollide check your position relative to the colided object. you can just make sure your y from the base of the object is higher then the y of the colliders top position. keep in mind this mean u can kill the enemey going at him in a 45 angle from the side
Answer by robertbu · Jul 12, 2013 at 07:26 PM
Given that you are using a CharacterController and that your enemies have a collider, you can use the CharacterController.collisionFlags to detect what part of the character collided. The 'collisionFlags' can be queried from the controller. They are also returned by the Move() function. They provide you information about what side(s) of the controller were hit on the last move. Note they will also report a collision with the ground and other objects, so you will need to do some Raycasting() or some other detection method to verify what was hit.
Your answer
Follow this Question
Related Questions
Player stucken in the wall while jumping 2D 1 Answer
Network Client does not Groundcheck 0 Answers
Jump Not Working 1 Answer
Why my variable istn't always used? 2 Answers
Applying Damage 5 Answers