- Home /
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?
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 :)
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.
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.
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
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?
- how is the player moving towards 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.
Answer by kiranbaby14 · Mar 11, 2020 at 05:42 AM
I would suggest you to use OnTriggerEnter( ) instead of OnControllerColliderHit( ).
Your answer
Follow this Question
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