Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference Between OnCollisionEnter () and OnTriggerEnter()

    • 0
    • 4
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 17.1k
    Comment on it

    OnCollisionEnter () and OnTriggerEnter() both are the methods of the Collider Class. Both are used to detect collisions when colliders enter the collision but both perform differently and cause different events.

    OnCollisionenter() is used to create Collisions between objects. Gameobjects collide with each other and get repelled by their forces.
    To use OnCollisionEnter(), a collider must be attached to the gameobject and its IsTrigger property should unchecked and in order to receive a physical collision event one of the colliding gameobject must have rigid body attached to itself.
    Code Example-

        void OnCollisionEnter()
        {
            Debug.log(Collided);
        }
        void OnCollisionEnter(Collision obj)
        {
            If(obj.gameobject.name==myObject)
            {
                Debug.log(Collided with+obj);
            }
        }
    
    In above example code, a Collision Class is passed as parameter for OnCollisionEnter () which contains information about the other colliding object, contact points, relative velocity, rigid body of colliding object etc. If we dont need any such information we can use use OnCollisionEnter () without any parameter.


    OnTriggerEnter() is used to detect collision but it does not act as solid body , rather allows the gameobjects to pass through them. To use OnTriggerEnter() , the IsTrigger property of the collider should be checked. To get trigger event to work properly a rigid body must be attached to one of the colliding gameobject.
    Code Example-

    void OnTriggerEnter()
    {
        Debug.log(Triggered);
    }
    void OnTriggerEnter(Collider obj)
    {
        If(obj.gameobject.name==myObject)
        {
            Debug.log(Triggered by+obj);
        }
    }
    
    In above example code, a Collider Class is passed as parameter in OnTriggerEnter() which contains the information about the triggering gameobject, if you want trigger to work for all gameobjects passing through it then eliminate the parameter.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: