Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • OnTrigger Event in unity

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 572
    Comment on it

    I am using OnTrigger event in my game, I have one player moving on a plane ,And i have mark 3 transform empty object on the plane, Start ,Centre and End. Now i have given tags to these 3 specific positions.

     When my player passes through these points it will give me trigger event. Now my player is moving along z direction, When it cross the centre transform position then a new prefab of a plane is generated.

     I have used OnTriggerExit funtion, Now when my player exit the following tag then it will through this event. 

    You can use 

    OnTriggerEnter, OnTriggerStay, and OnTriggerExit. also

     

    A simple demonstration of the event by this example.

    void OnTriggerEnter(Collider player)
    
    {
    
            if(player.gameObject.tag == "centre" && gen == true)
    
    {
    
                b = Instantiate(Resources.Load("Floor"), new Vector3(0,0,0), Quaternion.identity) as GameObject;
                Vector3 h = endpoint.position;
                h.z += 50f;
                b.transform.position = h;
    
      }
    
    }
    
    void OnTriggerExit(Collider player)
    
    {
    
            if(player.gameObject.tag == "centre" && gen == true){
    
                 Destroy(b.gameobject);
    
    }
    
    }


    now here i am checking whether my collider player gets the tag centre, if yes then it will instantiate the new prefab.

    In this way, we can use these events and can perform various functions. 

    In same way, we can use OnCollision event 

 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: