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. Dismiss Notice

Collider Help

Discussion in 'Scripting' started by VisualTech48, May 10, 2014.

  1. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Hi, I'm trying to make a that when the BoxCollider hits the wall, that it changes "IsColliding = true". The code won't simply work.
    Yes, I added a new tag, and the walls are asigned to the tag

    Code:
    Code (csharp):
    1.  
    2. function OnTriggerEnter (theCollider : Collider)
    3. {
    4.  if(theCollider.tag == "PlCol")
    5.  {
    6.  IsColliding = true;
    7.  }
    8. }
    9.  
    10. function OnTriggerExit (theCollider : Collider)
    11. {
    12.  if(theCollider.tag == "PlCol")
    13.  {
    14.  IsColliding = false;
    15.  }
    16. }
    17.  
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    One of the objects in the collision needs to have a rigidbody attached, according to this.

    Try putting a Debug.Log statement in both of the OnTriggerEnter calls to see if it's registering the collision at all.
     
  3. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    I have tried puting rigidbody to the walls and nothing. And yes, I have tried "Debug.Log" and it doesn't detect it at all. If you need more detailed info just ask
     
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Is either set of colliders marked as "Is trigger"? If not then OnTriggerXXX events won't get called, and you will need to use the OnCollisionXXX events instead.
     
  5. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Don't want to sound rude but I'm not that stupid. Of course I set on trigger the box. OnColission? Can you give me a code example, because I'm away and low on internet so I can't seach currently. And yea, Thank you ;)
     
  6. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Then please list everything that you already know so that I can avoid offending you while trying to help. You're welcome :)
     
  7. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    My mistake. Anyway, this is the current code, and it doesn't work :
    Code (csharp):
    1.  
    2. function OnCollisionEnter (Col : Collision)
    3. {
    4.  if(Col.gameObject.name == "Wall_Normal_01")
    5.  {
    6.  drawGUI = true;
    7.  Debug.Log("Test");
    8.  }
    9. }  
    Note: The Collider (BoxCollider) is attached to a spotlight, and that spotlight, contains the script.
    $Slike_Forum.png
     
  8. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    I seem to have missed why you've switched from OnTriggerEnter to OnCollisionEnter. Either way, OnCollisionEnter is not sent for triggers. You can see which messages are sent by referring to the matrix at the bottom of this page.

    In any case, to settle this issue, here's a short script that will most definitely work.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TriggerVisualizer : MonoBehaviour {
    5.  
    6.     void OnTriggerStay(Collider col)
    7.     {
    8.         Debug.Log(string.Format("Trigger colliding at time {0} with object {1}", Time.time, col.gameObject.name));
    9.     }
    10. }
    11.  
    Attach this to a game object that has a trigger collider attached and a rigidbody. Set the rigidbody to isKinematic. Create a wall, or whatever, that has a collider attached (trigger or non-trigger, but it does not need to have a rigidbody). Push play, open the Scene window and drag the trigger object overtop of the wall. The messages will be sent.

    If you need any more clarification feel free to ask!

    Erik
     
  9. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Will try, thanks for help :D