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

How do i get a object to detect collision while not containing physics

Discussion in 'Scripting' started by Sickan, Oct 2, 2014.

  1. Sickan

    Sickan

    Joined:
    Oct 2, 2014
    Posts:
    4
    Hi i is there some one out there who could help me with this?

    I have a cube that is "howering" and i can move araound around my player and it´s rigidbody is set to iskinematic = true.
    The problem is that i would like it to go throu objects that has physics in them like a rigid body and a colider of some kind and i want my "howering" item to detect colision so i can´t simply set the colision detection to false or can i? or shouls i use a different kind of colision detection? or perhaps i should be approtching this problem in a completely diffrent way?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Sickan

    Sickan

    Joined:
    Oct 2, 2014
    Posts:
    4
  4. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    I think what you need is a trigger. Set the collider to Is Trigger in the Inspector. This will allow things to pass through it while still detecting when the objects collide. You would then use one of the following methods to check for collisions:
    Code (CSharp):
    1. void OnTriggerEnter() {
    2. }
    3.  
    4. void OnTriggerStay() {
    5. }
    6.  
    7. void OnTriggerExit() {
    8. }
     
    Sickan likes this.
  5. Sickan

    Sickan

    Joined:
    Oct 2, 2014
    Posts:
    4
    Thanks i will test that :) to use it my object neds to be set to isTrigger = true and i can do it in code? :)
     
  6. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    To set a collider to be a trigger, you don't need code. Go to the inspector, look for the collider component, and then select the Is Trigger box. The code I wrote before was to check for collisions.

    OnTiggerEnter checks for when an object first enters the trigger.
    OnTriggerStay checks for when an object stays inside the trigger for a certain amount of time.
    OnTriggerExit checks for when an object exits the trigger.

    It's up to you to decide what you need, but my guess is that you want OnTriggerEnter.
    Here's an example:

    Code (CSharp):
    1. // this script is attached to the gameObject that IS a trigger
    2. // in your case, the hovering cube
    3.  
    4. void OnTriggerEnter(Collider col) // Collider is a type, col is a variable name
    5. {
    6.     if (col.gameObject.name == "Player") // checks if the name of the object it is colliding with is "Player"
    7.     {
    8.         // do something to the player
    9.     }
    10.  
    11. }
    Instead of col.gameObject.name, you could also do col.name.
    Similarly, col.gameObject.tag or col.tag.
     
    Sickan likes this.
  7. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Something to note is that one of the objects colliding has to have a rigidbody. So, if you have a sphere pass through the cube and the sphere has a rigidbody, then the cube - which is a trigger - will detect the collision.
     
    Sickan likes this.