• 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 pedrofernandes · Nov 03, 2011 at 02:22 AM · collision detection2d-gameplayspritemanager2

How to hit enemies with a scream

Hi people. I'm making a 2d game for android using sprite manager. In the game the player has to scream in order to kill enemies. I was wondering if somenone could give me an advice on the best way to do this. (i'm new to unity and was following tutorials and books up until now. I thought at first that i could test collision with a scream sprite, but i'm having difficulties figuring out where in the hierarchy the scream would be and where i would insert the script.)

Comment
Add comment · Show 4
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 syclamoth · Nov 03, 2011 at 02:23 AM 1
Share

For something like this, you'd test for proximity, and then maybe add some kind of distance falloff (depending on what the scream is supposed to be). I wouldn't be using collisions for this kind of thing.

avatar image pedrofernandes · Nov 03, 2011 at 05:34 AM 0
Share

That really sounds like a better approach. Will try it, thanks!

avatar image sam32x · Nov 03, 2011 at 05:40 AM 0
Share

put a collider on an empty gameobject, then instantiate it when the player screams and put code to make it move in function Start()

avatar image syclamoth · Nov 03, 2011 at 05:43 AM 0
Share

If you were going to do that, you'd be better to use Physics.OverlapSphere ins$$anonymous$$d.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by syclamoth · Nov 03, 2011 at 06:36 AM

Ok, fine, since there seems to be some call for detailed answers here-

To check for objects in a proximity, use Physics.OverlapSphere to get an array of colliders which are in range.

First off, make all the enemies which can be affected by this scream attack are on a layer together, so that your OverlapSphere will only return those which can be hit by it.

Then, use

 foreach(Collider hit in Physics.OverlapSphere(screamOrigin.position, screamRadius, screamLayers)
 {
     float damagePercentage = Vector3.Distance(screamOrigin.position, hit.transform.position) / screamRadius;
     hit.SendMessage("ScreamDamage", damage * damagePercentage);
 }

Then, on each object that can be hit by the scream attack, implement a function with these characteristics-

 void ScreamDamage(float damage)
 {
     // deal damage to the object
 }
Comment
Add comment · Show 3 · 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 pedrofernandes · Nov 03, 2011 at 06:41 AM 0
Share

Thanks for the help! Will try that and post the progress here!

avatar image pedrofernandes · Nov 03, 2011 at 10:56 PM 0
Share

Worked as a charm syclamoth, ty very much. Im losing about 2 frames when i scream though, i'm guessing it's about the layers, i didn't use the parameter. For what i've read it's an integer parameter of OverlapSphere and i couldn't reference it yet. Trying now.

avatar image syclamoth · Nov 03, 2011 at 11:00 PM 0
Share

No, if you use a public Layer$$anonymous$$ask (ins$$anonymous$$d of an int), you get a neat drop-down menu for the layers. What do you mean "losing about 2 frames"? Does it hang? The layermask should fix at least some of that...

avatar image
0

Answer by DaveA · Nov 03, 2011 at 06:18 AM

"For something like this, you'd test for proximity, and then maybe add some kind of distance falloff (depending on what the scream is supposed to be). I wouldn't be using collisions for this kind of thing."

or

"put a collider on an empty gameobject, then instantiate it when the player screams and put code to make it move in function Start()" but "If you were going to do that, you'd be better to use Physics.CheckSphere instead."

Answer the question guys.

Comment
Add comment · Show 1 · 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 syclamoth · Nov 03, 2011 at 06:28 AM 0
Share

What, like you just did?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast in 2.5D for bullet targeting 1 Answer

Background not Crushed 1 Answer

How to account for player's speed when firing projectiles 0 Answers

2D sprite in 3D game 2 Answers

how to control amount of force taken from the surface 0 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