Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 anamta93 · Feb 24, 2016 at 07:07 PM · collisiontriggercolliderscollision detectiontriggers

get only one colliding body

I am making a game where there are melee troops.

The problem is that; when one collide with two, it hits both of them together because they both of the same scripts. I want my players to hit only one.

Options i think might help: -Raycast -Front Collider (might cause the same problem again)

Can you please help me with the techniques that can be used ?

Thank you in advance :)

Comment
Add comment · Show 1
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 Tulsisvt · Feb 24, 2016 at 09:41 PM 0
Share

Could you clarify the question? I'm not sure what you mean by it hits two of them together. do the troops collide with each other? Did you try using tags on each gameobject to filter out the collisions?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Feb 26, 2016 at 04:04 AM

I guess that you simply have your attack code inside OnCollisionEnter / Stay which of course would run the code for every object your troops collider with. Here are some possible solutions depending on what behaviour is wanted:

  • Inside OnCollisionStay you simply attack the object you collided with (if it's "attackable") and then set a timeout / cooldown value to prevent more attacks. After that cooldown it automatically attacks again as long as it still collides with a target.

  • Use OnCollisionEnter (or any other method) to set the current target if you don't have one yet. In OnCollisionStay you only attack an object if it's the same as you stored as current target. This is a lock-on-target mode. You would need to add a way to clear the current target. This could be done by checking the distance to the target object. If the target moves too far away (checked in Update) you simply set the current target to null so it can pick a new one.

  • Both ways could be done without any collision checks. It depends on what targetting behaviour you want. Usually you would use either Physics.OverlapSphere or a large sphere trigger on your unit to get a list of possible targets which are in range and to pick one of them based on some targetting criteria. The most common one is picking the nearest enemy unit. The actual attack procedure is usually as follows: Once you picked a target you move towards the target. Once the distance to the target is smaller or equal to the attack range you stop moving and attack the enemy. After that you start a cooldown to wait for the next attack. If the enemy moved out of the attack range start moving again.

In the end there are endless behaviour possibilities and how you could implement those. For the attack sequence you could use a Coroutine however it's not necessary.

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 AndrewBilotti · Feb 24, 2016 at 07:10 PM

Well, make different scripts :)

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 pankrac2 · Feb 25, 2016 at 02:56 PM

Maybe test if one is moving or performing an attack, can't really say because i don't know how you do it :)

Pavel

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

6 People are following this question.

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

Related Questions

Trying to make object turn red OnTriggerEnter 0 Answers

Does OnTriggerStay don't detect a collision with a non-trigger collider? 1 Answer

How do I use colliders and/or triggers to end the game? 1 Answer

changing gravity OnTriggerEnter 1 Answer

How to single out specific colliders from another object 1 Answer


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