Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cashews · Jul 12, 2013 at 05:44 PM · jumpplatformerdamageattack

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 12, 2013 at 06:04 PM 0
Share

What kind of objects is your character and your enemies? Character controllers? Rigidbodies?

avatar image Cashews · Jul 12, 2013 at 06:55 PM 0
Share

@robertbu $$anonymous$$y Player has a Character Controller. I currently have my enemies as Rigidbodies but may change that if it becomes necessary

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markedagain · Jul 12, 2013 at 07:05 PM 0
Share

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

avatar image Cashews · Jul 12, 2013 at 07:23 PM 0
Share

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?

avatar image markedagain · Jul 12, 2013 at 07:26 PM 0
Share

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

avatar image Cashews · Jul 12, 2013 at 09:43 PM 0
Share

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?

avatar image Cerbion_ · Jul 12, 2013 at 10:50 PM 0
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges