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
1
Question by PeteZa94 · Nov 28, 2016 at 08:04 AM · c#collisiondeathdead

Die on collision from the front only

Hello.

I've got this script to detect when a player touches a game object with the tag "Enemy", he dies. I want it so that he only dies when he touches the "Enemy" from the front and sides, but when he's on top of them, he should be ok. Here's the script:

 private void OnControllerColliderHit(ControllerColliderHit hit)
     {
         if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
             Death ();
     }

Does anyone know how would I go about doing that?

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 PeteZa94 · Nov 28, 2016 at 11:21 AM 0
Share

bump please

avatar image Mehul-Rughani · Nov 28, 2016 at 12:10 PM 0
Share

you can also use different colliders with tags for example side collider with different tag and top collider with different tag

Hope it will help you :)

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MrFirster · Nov 28, 2016 at 01:01 PM

I would do it the following way.

  • Add a trigger collider to your player and advance it to the front and the sides

  • write a collision script for the enemy in which you capture the hit of the triggered player collider

that should give you the effect you want.

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 MrFirster · Nov 28, 2016 at 01:01 PM

Add a collider to your player and set it as a trigger. Now let the collider of your enemy detect a collision on that player trigger collider.

YOu can now adjust the collider of the player exact the way you want.

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 aditya · Nov 28, 2016 at 12:49 PM

 if(hit.gameObject.tag == "Enemy" && transform.position.y > hit.gameObject.transform.position.y){
     //Player above the enemy
 }

This means simply check the y position of enemy and player

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 Dakwamine · Nov 28, 2016 at 12:51 PM

You will have to work with the bounds of your two colliders:

  • What is a bound: https://docs.unity3d.com/ScriptReference/Bounds.html

  • How to get it: https://docs.unity3d.com/ScriptReference/Collider-bounds.html

In your player and your Enemy scripts, you have to save to a variable their bounds from the previous frame:

 // Public accessor for previous bounds
 public Bounds PreviousFrameBounds { get { return previousFrameBounds; } }
 private Bounds previousFrameBounds;
 
 void LateUpdate()
 {
     previousFrameBounds = GetComponent<Collider>().bounds;
 }

Then, when the two objects collide, in OnControllerColliderHit(), compare the bounds. With the previous bounds ( previousFrameBounds) and the current bounds, you will be able:

  • to calculate the "velocity" of the player and the Enemy by using Bounds.center,

  • to calculate their relative movement:
    • how is the player moving towards the Enemy?
      • From the top of the Enemy?

      • From the sides of the Enemy?

  • to compare whether the player is more or less aligned on top of the Enemy or not,

  • etc.

From there, the rest is up to you because the logic (maths) behind the detection of death depends on your game design!

One of the most annoying thing to take into consideration is the speed of the two objects. If they collide at very high speed, you may get wrong results in the death detection if you don't care enough of your logic. But this method, if well designed, is still safer than using multiple colliders as triggers.

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 kiranbaby14 · Mar 11, 2020 at 05:42 AM

I would suggest you to use OnTriggerEnter( ) instead of OnControllerColliderHit( ).

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why isn't my Death Script Working? 3 Answers

How to move an object over a distance in a direction? 2 Answers

Mesh deformation with collisions 2 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