Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Any way to differenciate between two colliders during a collision

Discussion in 'Scripting' started by d-ostapa, Aug 4, 2018.

  1. d-ostapa

    d-ostapa

    Joined:
    Jun 23, 2016
    Posts:
    21
    Hi. Im trying to make a simple arena game where two players (currently one player and a simple navmesh agent) try to avoid while also hit eachother to deal damage. Currently everything works the way it should. Except that both players deal the same damage to eachother when they collide, since their scripts on each for damage take away health from a health variable every time a collision happens between them. I was wondering if their was a way to either see which object had the most force, or which one hit first, etc. Because after you hit the enemy 5 times, you also die.
    Attack/Damage script
    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine;
    3.  
    4. public class attack : MonoBehaviour {
    5.  
    6.     public GameObject destroy;
    7.     public GameObject player;
    8.     public float health = 50f;
    9.     public Slider healthbar;
    10.     public Slider playerhealthbar;
    11.  
    12.     private void OnCollisionEnter(Collision enemy)
    13.     {
    14.         if(enemy.gameObject.name == "enemy")
    15.         {
    16.             health -= 10;
    17.             healthbar.value -= 0.25f;
    18.  
    19.             if (health <= 0)
    20.             {
    21.                 Destroy(destroy);
    22.             }
    23.         }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. d-ostapa

    d-ostapa

    Joined:
    Jun 23, 2016
    Posts:
    21
    Not sure if i set it up wrong, but still my problem remains of unity detecting that they both touched, and when they touch it measures the force of both the objects colliding which outputs the exact same result. Whenever they touch unity thinks that the Player collider touched the Enemy collider, while also the Enemy collider touched the Player collider, causing both of them to run each of their if collide statements at the exact same time. And with Collision.Impulse whenever I try measuring the force I get the same result because of the same issue as earlier, causing me to not be able to do any greater than or less than.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're right. I would have assumed the impulses would be different, the more impactful body requiring more displacement compared to the slower body but that does not appear to be the case.

    You'll have to do something like store the previous velocity of the body and compare that value to the object you hit's previous velocity.