Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnTriggerEnter() vs OnCollisionEnter()

Discussion in 'Scripting' started by Brandon-Keeler, Nov 20, 2014.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    What is the difference between OnTriggerEnter() and OnCollisionEnter()? Also what are their implementations?
     
    NewSokrates likes this.
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Look at the API. It's there for a reason.
     
  3. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    I'd be amazed if this didn't come up in a Google search either.

    Having said that, OnTriggerEnter is called when a collider enters a trigger volume (a collider with 'Trigger' ticked) and OnCollisionEnter is called when there is a collision between colliders that aren't set to to 'Trigger'.

    That's simplifying a bit. This probably explains things a little more, along with the API: http://docs.unity3d.com/Manual/CollidersOverview.html
     
    Freudarian and DRRosen3 like this.
  4. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I was just wondering, because I have this compound PlayerController script, that does one thing when the character is Kinematic, and another thing when it isn't Kinematic. When my character isn't Kinematic, my character collides perfectly with the floor and stops moving, but when it is Kinematic, it passes right through the floor (which I don't want it to do). Do you think you can help me with that? This is my code...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     public GameObject player;
    6.     public float KineticMoveSpeed;
    7.     public float NormalMoveSpeed;
    8.     public float turnSpeed;
    9.     float moveHorizontal;
    10.     float moveHeight;
    11.     float moveVertical;
    12.     float KinematicTimeInAir;
    13.     float Acceleration;
    14.     bool spaceKeyDown = false;
    15.     bool isGrounded = true;
    16.     void Start()
    17.     {
    18.         KinematicTimeInAir = 0.0f;
    19.         Acceleration = -9.81f;
    20.     }
    21.     void Update ()
    22.     {
    23.         moveHorizontal = Input.GetAxis ("Horizontal");
    24.         moveVertical = Input.GetAxis ("Vertical");
    25.         if(Input.GetButtonDown("Jump"))
    26.         {
    27.             spaceKeyDown = true;
    28.             //Debug.Log("Space Key Pressed. Frame: " + Time.frameCount);
    29.         }
    30.     }
    31.  
    32.     void FixedUpdate ()
    33.     {
    34.                 if (player.rigidbody.isKinematic == true) {
    35.                 KinematicPlayerJump (KinematicTimeInAir);
    36.                         transform.Rotate (Vector3.up * moveHorizontal * turnSpeed * Time.deltaTime);
    37.                         transform.Translate (Vector3.forward * moveVertical * KineticMoveSpeed * Time.deltaTime);
    38.                 }
    39.                 else
    40.                 {
    41.                         NormalPlayerJump ();
    42.                         Vector3 movement = new Vector3 (moveHorizontal, moveHeight / NormalMoveSpeed, moveVertical);
    43.                         rigidbody.AddForce (movement * NormalMoveSpeed * Time.deltaTime);
    44.                 }
    45.         moveHeight = 0;//Make sure vertical movement is zeroed after normal jump sequence.
    46.         //Need to set KinematicTimeInAir = 0.0f, if the player collides with the ground
    47.     }
    48.  
    49.     void NormalPlayerJump()
    50.             {
    51.         if (isGrounded == true)
    52.         {
    53.             if (spaceKeyDown == true)
    54.             {
    55.                 spaceKeyDown = false;
    56.                 moveHeight = 9.8f * 1500f;
    57.             }
    58.             else {moveHeight = 0;}
    59.         }
    60.         else {moveHeight = 0;}
    61.         spaceKeyDown = false;
    62.     }
    63.     void KinematicPlayerJump(float time)
    64.     {
    65.         if (spaceKeyDown == true) {
    66.             float VelocityInitial = 9.8f;
    67.             float CurrentVelocity;
    68.             CurrentVelocity = VelocityInitial + Acceleration * time;
    69.             transform.Translate (Vector3.up * (CurrentVelocity) * Time.deltaTime);
    70.             KinematicTimeInAir += (float)(Time.deltaTime);
    71.         }
    72.     }
    73.     void OnCollisionEnter (Collision col)
    74.     {
    75.         if(col.gameObject.tag == "Floor")
    76.         {
    77.             isGrounded = true;
    78.         }
    79.     }
    80.  
    81.     void OnCollisionExit (Collision col)
    82.     {
    83.         if(col.gameObject.tag == "Floor")
    84.         {
    85.             isGrounded = false;
    86.         }
    87.     }
    88. }
     
    NewSokrates likes this.
  5. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    See the link I posted above: