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 Cutty_Flam · Jan 04, 2017 at 09:54 AM · collisioncollision detection

How to detect different types of collisions?

I've heard of different solutions for handling collisions but I have a sort of complicated scenario (I think). Take a multiplayer game with two turrets floating on asteroids in space which both fire bullets (each turret belongs to a different player). When turret1 enters the range of turret2, turret2 shoots at turret1 (ranges aren't necessarily equal). When the bullet collides with a turret, the turret takes damage.

I was thinking of implementing this with two colliders on each turret: one big circle collider to detect when another turret enters its range, and a small box collider to detect the bullets. Obviously then the bullets should have a box collider. And the same turret box collider used to detect bullets should probably be used to collide with the big circle collider. Here's a summarizing graphic:

alt text

The Y's represent whether the two colliders interact.

The problem is, the bullet will hit the big circle colliders before hitting the turret box collider. Then I'm also confused about all the possibilities with layers, isTrigger, kinematic colliders, and rigidbodies.

Can somebody suggest how I should go about this?

EDIT: In addition, I don't want friendly fire. So turrets and bullets from the same player have to ignore each other.

pic.png (22.1 kB)
Comment
Add comment
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

2 Replies

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

Answer by Creeper_Math · Jan 04, 2017 at 08:12 PM

Here's the two options for colliders. Collider (notrigger) or Trigger

Collider (with isTrigger set to false) is the default collider setting. It will use Unity Physics to bounce around and such. Note that colliders CANNOT detect Trigger colliders.

Trigger, like a Collider, also detects when other collider / trigger goes through. Note that triggers can detect both colliders and trigger colliders. Unlike the collider, a Trigger does not bounce around with physics, as if you just set up a box with a trigger instead of a collider, it will just fall through the floor.

Rigidbodies is for adding built in unity physics to the object, allowing it to fall due to gravity or adding force via script.

Now time to help implementing it with your goal... First of all, you don't need that larger circle for detecting when an enemy is within range. If you want to take a stab at complex coding, u can use if (Vector3.Distance(transform.position,OtherPlayer.transform.position) < Enemy Detection Distance , assuming this will only include just the two turrets.

If you don't want to use the Vector3.Distance, I'll include the other option here. The larger circle can have a trigger enabled, Note that this will pick up all objects, and that triggers use different scripting than colliders. To understand triggers more you can look at https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers

To make sure that each turret isn't detecting shells or their own turret, you will most likely need to include a bit of scripting to stop the script from executing for other shells. A way to do this does include a Vector3.Distance however, but it's a bit easier to get used to.

 public bool EnemyInRange;
 float AlliedExceptionRange = 1;
 void OnTriggerStay(collider c) {
  // 'collider c' sets a reference of 'c' to the collider's gameobject
  // 'OnTriggerStay' will execute every frame that the enemy is in range.
       if (c.tag != "Shot" && Vector3.Distance(transform.position,c.transform.position) > AlliedExceptionRange) {
             EnemyInRange = true;
       }
 }
 void Update() {
       if (EnemyInRange) {
             // All of your shooting stuff, just make sure that your recharge is placed out of the "if" statement
       }
 }
 void LateUpdate() {
       EnemyInRange = false;
       // this resets the enemyinrange after every frame to make sure that the turret ceases to shoot once the enemy in out of the range
 }

This would go onto the larger sphere script. The values I set there would work if the center of the larger circle is on the turret, otherwise incresing the "AlliedExceptionRange" would make the script not include any entities out of that range

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 Majesteit · Jan 05, 2017 at 06:57 AM

Shooting range: You can use a trigger collider for this. This doesn't actually collide, only check for collision. You can handle this easily with code, use the OnTriggerEnter() for that.

Taking damage from bullet: You can use a normal collider for this, use the OnCollisionEnter() event. If this events gets triggered, check if the collision is a bullet, and then take damage.

Comment
Add comment · Show 2 · 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 Cutty_Flam · Jan 05, 2017 at 05:47 PM 0
Share

What if I wanted to make the bullet pass through a max of 3 turrets and deal damage to all of them? Would OnCollisionEnter() still work?

avatar image J0hn4n Cutty_Flam · Apr 01, 2017 at 12:40 AM 0
Share

You can make the bullet trigger, make a integer to store max number of penetrations, and every time when the bullet exits from that collider just sustrac 1.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make arrow stick to environment ? 3 Answers

Strange Collisions (...as there should be none!) 0 Answers

Enemy not taking damage on collisions. 2 Answers

Physic based golf game - ball bouncing off the connection of colliders on flat surface 2 Answers

NavMesh Collision Detection? 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