Search Unity

Colliding without physics in Unity: is it possible to put OnTriggerEnter in the moving character?

Discussion in 'Scripting' started by louisXV, Oct 8, 2015.

  1. louisXV

    louisXV

    Joined:
    Oct 8, 2015
    Posts:
    10
    My main question is: is it possible to put OnTriggerEnter in a script that goes attached to the moving character instead of to the trigger areas/objects? Let me explain my exact need.

    I want to detect the collision between an object called MyCube and another called MyOther, but without MyOther stopping MyCube to go on. It is, MyCube should not be stopped or bounced. It should actually just move on, without any physics reaction. Yes, I know that it can be achieved by using Triggers.

    More specifically, I should define MyOther's bounding box as a Trigger and then define MyCube as a kinematic rigidbody. Then, I should add a script to MyOther with a function like, for instance, OnTriggerEnter. However, in my scene I will have thousands of objects like MyOther, i.e. working as triggers, and also all the action to be performed when colliding would be performed on the movign character itself. So I would like to retrieve the collision not trough MyOther, but trough MyCube.

    1) I thought that I could put OnTriggerEnter in a script attached to the moving character, but when I do that no collision is recognized. No trigging occurs. Is such a thing possible?

    2) Otherwise, is there another way trough which I could achieve what I have described?
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    For what I know, you need to have a collider with 'isTrigger' enabled on both/all objects you want the OnTriggerEnter method to work on, and at least one needs a Rigidbody. I had issues with that in Unity 4, where I forgot to attach Rigidbody and it did not want to work, though I do not know if there's something new in Unity 5 that kinda helps out a little.
     
  3. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I suppose there is a layer option for Collider type.
     
  5. louisXV

    louisXV

    Joined:
    Oct 8, 2015
    Posts:
    10
    Many thanks for your replies and sorry for the late feedback (I had to travel for work). So, I am still at the same place. It does not matter if I put Rigidbody in the Agent or in MyCube (the agent) or MyObject (the trigger). The detection only works if the OnTrigger events are in scripts attached to MyObject, not to MyCube (even if MyCube has IsTrigger true). In my setting, I don't use layers, i.e. there is no specification there that can be messing this.