Search Unity

Animation by Trigger of box collider isTrigger

Discussion in 'Animation' started by Clik_Mazer, Sep 6, 2020.

  1. Clik_Mazer

    Clik_Mazer

    Joined:
    Apr 19, 2019
    Posts:
    4
    Hello,

    I am trying to make a simple cube with box collider as isTrigger. I have searched endless amount of youtube videos and online forums and cannot find a simple example or way of doing this. I made a cube with istrigger, and want that to when passed through by player, make another platform play an animation. I am new but learning about Unity and scripting very quickly. If anyone could help me, that would be greatly appreciated. If you need more details, please let me know.

    Just to confirm what I am looking for, player enters a isTrigger cube, on a platform, and to move another platform from across the map to me. I made the animation already and just need to call it from OnTriggerEnter and to define what I want moved.

    Thanks
     
  2. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    You need to add a custom script to the controller that handles the messages OnTrigger.

    https://docs.unity3d.com/ScriptReference/Collider.html

    Here's an example script I wrote which takes a door that has a collider component added with isTrigger flagged true. When the player gets near the door the animator parameter is updated so the door animation can play. On exit the door closes.
    Code (CSharp):
    1. public class DoorTrigger : MonoBehaviour {
    2.     Animator m_animator;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.         m_animator = this.GetComponent<Animator>();
    7.     }
    8.  
    9.  
    10.     void OnTriggerEnter(Collider obj)
    11.     {
    12.         if (m_animator)
    13.         {
    14.             m_animator.SetBool("isNearDoor", true);
    15.         }
    16.     }
    17.  
    18.     void OnTriggerExit(Collider obj)
    19.     {
    20.         if (m_animator)
    21.         {
    22.             m_animator.SetBool("isNearDoor", false);
    23.         }
    24.     }
    25. }
     
    Clik_Mazer likes this.
  3. Clik_Mazer

    Clik_Mazer

    Joined:
    Apr 19, 2019
    Posts:
    4
    This works but I want to as player, walk through collider and cause something else to happen. For example, a platform that is not near, to change position or do animation by walking into a box collider but now the trigger of that platform.
     
  4. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Correct
     
  5. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    So I would add a public class member of type Animator, then once the component is added to your collider object drag the object that you want to have animate ( which has the animator component on it) onto this script's variable.

    I would provide you with the code example, but I'm not near a computer for the next 4 days.
     
  6. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Hahahahhahaha, that's sucks