Search Unity

Question Multiple objects, one animation?

Discussion in 'Scripting' started by erdirden, Oct 19, 2022.

  1. erdirden

    erdirden

    Joined:
    Sep 29, 2022
    Posts:
    15
    I want to move multiple characters using a single animation. I tried to use loop for this but failed. Can you help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ses : MonoBehaviour
    7. {
    8.     AudioSource audioSource;
    9.     public Animator animator;
    10.     public GameObject[] karakul;
    11.     void Start()
    12.     {
    13.         audioSource = GetComponent<AudioSource>();
    14.         //animator = GetComponent<Animator>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22.     public void OnTriggerEnter(Collider other)
    23.     {
    24.         if (other.gameObject.tag == "sayiCollider")
    25.         {
    26.  
    27.             audioSource.Play();
    28.             foreach (var item in karakter)
    29.             {
    30.                 animator.SetBool("clap", true);
    31.             }
    32.         }
    33.         else
    34.         {
    35.             animator.SetBool("clap", false);
    36.         }
    37.    
    38.     }
    39.  
    40. }
     
  2. erdirden

    erdirden

    Joined:
    Sep 29, 2022
    Posts:
    15
    edit: images
     

    Attached Files:

    • a.png
      a.png
      File size:
      7.8 KB
      Views:
      104
    • b.png
      b.png
      File size:
      5.7 KB
      Views:
      115
    • d.png
      d.png
      File size:
      796.5 KB
      Views:
      110
  3. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Else clap being false means that whoever is not colliding is not clapping. You should have like a clap break timer which everyone has. And on the collision everyone’s clap break timer is set to 4 seconds or something. After clap break is less than 0 you can set bool animator false on no collision tag.
     
  4. erdirden

    erdirden

    Joined:
    Sep 29, 2022
    Posts:
    15
    That's what I did but it didn't detect collision. Sound and animation did not work.

    Code (CSharp):
    1. [SerializeField] private Animator[] _animators;
    2.  
    3. private void OnTiggerEnter(Collider other)
    4. {
    5.    bool isSayi = other.CompareTag("sayiCollider");
    6.  
    7.    if (isSayi) audioSource.Play();
    8.    else audioSource.Stop();
    9.  
    10.    for (int i = 0; i < _animators.Length; i++) _animators.SetBool("clap", isSayi);
    11. }
     
  5. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Did you check if Animation has exit time?
    Does animator show state change to clap when playing the game?
     
  6. erdirden

    erdirden

    Joined:
    Sep 29, 2022
    Posts:
    15
    It worked this way. But only one animation works.


    Code (CSharp):
    1. public void OnTriggerEnter(Collider other)
    2. {
    3.     if (other.gameObject.tag == "sayiCollider")
    4.     {
    5.  
    6.         audioSource.Play();
    7.         animator.SetBool("clap", true);
    8.     }
    9.     else
    10.     {
    11.         animator.SetBool("clap", false);
    12.    }
    13.  
    14.     }
    Animator;