Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Enabling multiple object animations with one collider?

Discussion in 'Scripting' started by SinaBanana1, Nov 3, 2019.

  1. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    Hello everyone,

    I am trying to build a scene where I have multiple game objects and each of them has their own animation that is triggered separately upon collision with the main collider / player. So far it is working well with one game object. Once I try adding several ones, all animations will start, although it is only entering the trigger zone of say gameobject1.

    Any ideas? My script is below.

    Thank you!


    Code (CSharp):
    1. sing System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Switch2 : MonoBehaviour
    6. {
    7.  
    8.     public GameObject ObjectToEnable;
    9.  
    10.     void Start()
    11.     {
    12.         ObjectToEnable.SetActive(false);
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.     {
    17.         ObjectToEnable.SetActive(true);
    18.  
    19.     }
    20.  
    21.  
    22.  
    23.  
    24. }
     
    Last edited: Nov 3, 2019
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    I'm not sure if I understand your logic behind this pattern correctly, but; maybe you could implement this so, that you have OnTriggerEnter in your objects, and when your player enters a trigger, it will start whatever it needs to start in that specific object instance.

    i.e. something like this:
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2. {
    3.     // If it's the player (detect with tag you have set.)
    4.     if (other.tag == "Player")
    5.     {
    6.         // Flip a trigger in this Animator instance to start animation playback.
    7.         animator.SetTrigger("reactionAnim");
    8.     }
    9. }
    animator would be of type Animator and something you assign automatically with code or use Inspector to do it.
     
    SinaBanana1 likes this.
  3. SinaBanana1

    SinaBanana1

    Joined:
    Oct 20, 2019
    Posts:
    8
    Thank you that was very helpful!