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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

A box collider and a sphere collider detecting different things on same object

Discussion in 'Editor & General Support' started by _gish, Jan 5, 2016.

  1. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Alright, so I've tried doing some research on this before posting, but wasn't able to find a solution.

    Basically, I have my enemies walking back and forth, and will detect the player (and kill him) if the player collides with the enemies box collider.

    I am now trying to add a sphere collider with a fairly large radius, that will detect when the player is in range, and cause the enemy to chase him.

    The problem is (as you probably guessed) that when my player contacts the large spherical collider, the enemy kills him.

    I read that Unity will not allow you to have multiple colliders doing different things on the same object (not sure if this is true), so I also tried creating a seperate sphere object (which has its own sphere collider) and making this a child object of my enemy (and then attaching my aggroRange script to that) I got the same results >_<

    Has anyone had this problem before?
     
  2. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    Are you checking which collider has been hit and adjusting the behaviour appropriately?
    E.g. (psuedo code)
    Code (CSharp):
    1. if (collider == sphere)
    2.   chasePlayer();
    3. else
    4.   killPlayer();
    5.  
     
  3. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Thank you for the response.

    I currently have a OnTriggerEnter method that is on a script attached to my enemy which checks for collisions. I just tried implementing your suggestion, so that the enemy must also be colliding with a box collider when the "Player" tag is found, but no changes. The player still dies when he comes into contact with the sphere collider.

    Code (CSharp):
    1. //Collision detection
    2.     void OnTriggerEnter(Collider c)
    3.     {
    4.         //If player is hit by enemy, deal damage to player
    5.         if (c.tag == "Player" && c is BoxCollider)    
    6.         {
    7.             canAttackPlayer = true;
    8.             animator.SetBool ("Can Attack Player", true);//Attack by changing canAttackPlayer bool in Animator
    9.        
    10.             Debug.Log ("Player is hit");
    11.             c.GetComponent<DamageDealer> ().TakeDamage (10);//Deals 10 damage (Calls TakeDamage method from Entity class)
    12.         }
    13. }
     
  4. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
    Have a look at the attached Unity Project, is this something you're after?
    Also I recommend having a look at the example AngryBots project, the enemy robots contain the behaviour you've described to wake up and follow the player when the collider is triggered.
     

    Attached Files:

  5. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Thank you for the response,

    I ended up using raycasting instead of my sphere collider mehod.

    Ended up being a bit more work, but it was worth it :)