im trying to make an fnf remake, but i am stuck at the enemy animations and all i have is the arrows disappear when colliding with the enemy’s buttons. here is the script i attached to the buttons for the arrows to be destroyed:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroy : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.name == "GameButton")
{
Destroy(col.gameObject);
}
}
}
but how would i go about playing one of 4 animations when this happens. keep in mind this is a seperate game object to the enemy arrows and buttons, how would i trigger an animation when these gameobjects are destroyed? Would i need to make a different script for the enemy player singing animations or would i attach this script to them? thank you, Ep8s.
First of all, don’t be so quick to just destroy the object. Sometimes when you destroy an object it causes a problem with another object that was “interacting” with it, or you may want the object to finish playing a complete sound before it disappears. One way to do that is using a boolean to keep track whether it is time to destroy an object, like bool DestroyMe = "true"
Or, you could use a “controller” object (something that I like to do), which is an object that handles all the creating, destroying, sound-playing, and all the other stuff like that. To do that, you make a script on your “game controller” object, and in the Awake function, you put DontDestroyOnLoad(this.gameObject);
So, now that object won’t be destroyed automatically when you change game-scenes, so it can play background music without stopping while loading different scenes, or trigger animations to play on different objects, keep track of the amount of enemies left in the scene, or destroy an enemy, etc.
Brackey’s explains that in this tutorial at 1 minutes 50 seconds into the video:
So you’ll need to use Unity’s Animator Controller and a script that accesses that controller.
…The video should explain it but here’s a basic example with some stuff you might need:
using UnityEngine;
public class AnimatorGetBool : MonoBehaviour
{
Animator m_Animator; // make a "holder" for an Animator called m_Animator
public GameObject animObject; //set the object in the Inspector that you want to control or interact with
int whichChoice = 0; //a variable for choosing a random choice
void Start()
{
m_Animator = animObject.GetComponent<Animator>(); //get the Animator during run-time (caching)
}
void OnTriggerEnter (Collider other) //called automatically when collision occurs
{
//if the colliding object is tagged Enemy...
if (other.gameObject.tag == "Enemy")
{
m_Animator.SetBool ("Explode1Play", true);} //Play an explosion sound or animation
whichChoice = Random.Range(0, 4); //choose a random number between 0 and 3 (4 numbers)
if (whichChoice == 0)
{
//do the 1st thing
}
if (whichChoice == 1)
{
//do the 2nd thing
}
if (whichChoice == 2)
{
//do the 3rd thing
if (whichChoice == 3)
{
//do the 4th thing
}
}
}
void Update()
{
}
}//END
*Note that an animation controller can do a lot more than just play animations. Using the Animator variables, “Animator Events” and scripts, you can trigger sounds to play at specific times during animations and much more.
i mean you know how in friday night funkin, the character opposing the player sings via ai? i already have the script that destroys the arrows on the opponent’s side but i need to figure out how to get the npc opponent to have up, down, left and right singing animations when for example, the ai opponent hits an up arrow, they sing up. like that.