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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Trying to set active children objects based on animation frame - NOT WORKING

Discussion in 'Animation' started by nicolascolaleo1, Jul 11, 2022.

  1. nicolascolaleo1

    nicolascolaleo1

    Joined:
    May 22, 2022
    Posts:
    6
    [CODE AT THE BOTTOM]
    Hello, I'm fairly new to unity so I'm still learning.
    I'm currently working on a 2d pixel art game and working on a unique enemy special attack which moves over a large portion of the screen. each frame is drawn and split all the frames into individual sprites, then put together frame by frame in an animation.
    So everything look good when I play the game, but the attack do not have any colliders - meaning it cannot collide with my player if it "hits" it, so this is the challenge I'm facing to set up.

    I want to set active (true / false) children in an object in which each gameobject holds a different collider shape (to follow the animation "movement"), so I tried to write the next code to make each frame switch active its children based on the current frame, but obviously, I'm missing something, but couldn't understand what am I doing wrong, now I understand this not might be the best way to achieve what I want but at least I'm trying to be original lol..

    my animation has 21 frames, and I want it to change the collider for the first 10 frames (meaning there are 10 gameobject children each holds their own collider shape), each frame throw setting active to true and false it's children which each holds a collider positioned for a specific frame.

    The error I'm currently facing when running is with the animation attachment, throw this error:
    hope anyone could help..

    edit: here is a mockup gif to have better understanding on what i'm trying to achieve:


    And here is an image of the game prefab I'm talking about (for the image i set active all the gameobjects so you could see each collider):


    edit 2: i thought about another approach to achieve this with my current knowledge , but i think it's too messy.. I thought about using the animation event system and make a function for each case, and in each frame add the specific function to switch off and on the colliders, but I just think it looks bad.. anyways hopefully I can get an answer - THIS IS WHAT I DID FOR NOW, AND IT WORKS GREAT, but it just feels like a bad approach..

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BossSmallEnergyBeamDestroyer : MonoBehaviour
    6. {
    7.     public GameObject BeamParent;
    8.     public int ColliderFrame;
    9.     private Animation anim;
    10.  
    11.     private void Awake()
    12.     {
    13.         anim = GetComponent<Animation>();
    14.     }
    15.     private void Update()
    16.     {
    17.         ColliderFrameChange();
    18.     }
    19.     private void FixedUpdate()
    20.     {
    21.         //Destroy(gameObject, 1.750f);
    22.     }
    23.     private void ColliderFrameChange()
    24.     {
    25.         //1.750F - small beam
    26.         //21 frames (1 / 21 = 0.0476190476190476)
    27.         if(anim["LeftBeamAnim"].normalizedTime >=0f
    28.             && anim["LeftBeamAnim"].normalizedTime < 0.047f)
    29.         {
    30.             ColliderFrame = 0;
    31.             SwitchCollider();
    32.         }
    33.         else if(anim["LeftBeamAnim"].normalizedTime >=0.047f
    34.             && anim["LeftBeamAnim"].normalizedTime < 0.095f)
    35.         {
    36.             ColliderFrame = 1;
    37.             SwitchCollider();
    38.         }
    39.             else if(anim["LeftBeamAnim"].normalizedTime >=0.047f
    40.             && anim["LeftBeamAnim"].normalizedTime < 0.095f)
    41.         {
    42.             ColliderFrame = 2;
    43.             SwitchCollider();
    44.         }
    45.             else if(anim["LeftBeamAnim"].normalizedTime >=0.095f
    46.             && anim["LeftBeamAnim"].normalizedTime < 0.142f)
    47.         {
    48.             ColliderFrame = 3;
    49.             SwitchCollider();
    50.         }
    51.             else if(anim["LeftBeamAnim"].normalizedTime >=0.142f
    52.             && anim["LeftBeamAnim"].normalizedTime < 0.190f)
    53.         {
    54.             ColliderFrame = 4;
    55.             SwitchCollider();
    56.         }
    57.             else if(anim["LeftBeamAnim"].normalizedTime >=0.190f
    58.             && anim["LeftBeamAnim"].normalizedTime < 0.238f)
    59.         {
    60.             ColliderFrame = 5;
    61.             SwitchCollider();
    62.         }
    63.             else if(anim["LeftBeamAnim"].normalizedTime >=0.238f
    64.             && anim["LeftBeamAnim"].normalizedTime < 0.285f)
    65.         {
    66.             ColliderFrame = 6;
    67.             SwitchCollider();
    68.         }
    69.         else if(anim["LeftBeamAnim"].normalizedTime >=0.285f
    70.             && anim["LeftBeamAnim"].normalizedTime < 0.333f)
    71.         {
    72.             ColliderFrame = 7;
    73.             SwitchCollider();
    74.         }
    75.         else if(anim["LeftBeamAnim"].normalizedTime >=0.333f
    76.             && anim["LeftBeamAnim"].normalizedTime < 0.380f)
    77.         {
    78.             ColliderFrame = 8;
    79.             SwitchCollider();
    80.         }
    81.         else if(anim["LeftBeamAnim"].normalizedTime >=0.380f
    82.             && anim["LeftBeamAnim"].normalizedTime < 0.428f)
    83.         {
    84.             ColliderFrame = 9;
    85.             SwitchCollider();
    86.         }
    87.         else if(anim["LeftBeamAnim"].normalizedTime >=0.428f
    88.             && anim["LeftBeamAnim"].normalizedTime < 0.476f)
    89.         {
    90.             ColliderFrame = 10;
    91.             SwitchCollider();
    92.         }
    93.         else if(anim["LeftBeamAnim"].normalizedTime >=0.8095f)
    94.         {
    95.             ColliderFrame = 12;
    96.             SwitchCollider();
    97.         }
    98.     }
    99.     private void SwitchCollider()
    100.     {
    101.         switch(ColliderFrame)
    102.         {
    103.             case 0:
    104.                 BeamParent.transform.GetChild(0).gameObject.SetActive(true);
    105.                 break;
    106.             case 1:
    107.                 BeamParent.transform.GetChild(0).gameObject.SetActive(false);
    108.                 BeamParent.transform.GetChild(1).gameObject.SetActive(true);
    109.                 break;
    110.             case 2:
    111.                 BeamParent.transform.GetChild(1).gameObject.SetActive(false);
    112.                 BeamParent.transform.GetChild(2).gameObject.SetActive(true);
    113.                 break;
    114.             case 3:
    115.                 BeamParent.transform.GetChild(2).gameObject.SetActive(false);
    116.                 BeamParent.transform.GetChild(3).gameObject.SetActive(true);
    117.                 break;
    118.             case 4:
    119.                 BeamParent.transform.GetChild(3).gameObject.SetActive(false);
    120.                 BeamParent.transform.GetChild(4).gameObject.SetActive(true);
    121.                 break;
    122.             case 5:
    123.                 BeamParent.transform.GetChild(4).gameObject.SetActive(false);
    124.                 BeamParent.transform.GetChild(5).gameObject.SetActive(true);
    125.                 break;
    126.             case 6:
    127.                 BeamParent.transform.GetChild(5).gameObject.SetActive(false);
    128.                 BeamParent.transform.GetChild(6).gameObject.SetActive(true);
    129.                 break;
    130.             case 7:
    131.                 BeamParent.transform.GetChild(6).gameObject.SetActive(false);
    132.                 BeamParent.transform.GetChild(7).gameObject.SetActive(true);
    133.                 break;
    134.             case 8:
    135.                 BeamParent.transform.GetChild(7).gameObject.SetActive(false);
    136.                 BeamParent.transform.GetChild(8).gameObject.SetActive(true);
    137.                 break;
    138.             case 9:
    139.                 BeamParent.transform.GetChild(8).gameObject.SetActive(false);
    140.                 BeamParent.transform.GetChild(9).gameObject.SetActive(true);
    141.                 break;
    142.             case 10:
    143.                 BeamParent.transform.GetChild(9).gameObject.SetActive(false);
    144.                 BeamParent.transform.GetChild(10).gameObject.SetActive(true);
    145.                 break;
    146.             default:
    147.                 BeamParent.transform.GetChild(10).gameObject.SetActive(false);
    148.                 break;
    149.         }
    150.     }
    151. }
    152.  
     
    Last edited: Jul 11, 2022
unityunity