- Home /
How do I make a spotlight make a model play an animation?
I have a monster model outside of a window. I've been trying to make it so the player's flashlight makes the monster, say, play a death animation. How do I do this? At first I attached a long rectangle that would collide with the monster, but that didn't work. Is there a simple way to do this? I have the monster waiting at the window playing an idle animation until the player shines the light. If there's a way, I would be very grateful because this is one of the main parts of my game.
Answer by suIly · Dec 01, 2020 at 02:22 PM
That's easy. Just add a Rigidbody and a Collider to the monster. Then add an Is Trigger Collider to the flashlight beam. In the monster script, type:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Flashlight Beam")
{
monsterAnimator.SetTrigger("PlayDeathAnimation");
}
}
Then in the Monster Animator, click on the Parameters tab, click the + button, and add a Trigger called "PlayDeathAnimation". Make a transition from the Idle Animation, with the condition being "PlayDeathAnimation".
Answer by Gavisoft · Dec 01, 2020 at 07:59 PM
@jacksonacademyashmore Thanks for responding! The only problem I see is that the console gets an error saying "monsterAnimator does not exist in the current context". Would I have to make a variable refering monsterAnimator to the animator? I assume I do. (Edit) I probably should have mentioned it, but It's a 3d game, but I'm sure the code works the same when I remove the "2D" off the ends of the code. I tried making a [SerializeField] private variable, but I didn't know what kind of variable I would need. Very sorry, I am new to coding.
Yes, make sure to remove the 2D out of the code, and make a variable for monsterAnimator. public Animator monsterAnimator. Next time, press Add Comment so I can get a notification when you reply so I will be faster at responding.