How to do a Spawn Animation
Hi all,
This is my second question (first in a while) and it's probably something incredibly simple that I'm being too dense to realise! (Newb here trying to figure out as much as possible by myself with learning C# and how to use Unity.)
I currently have a saved prefab that is being instantiated at the position of a game object selected at random from an array. I'm looking for a way to make some kind of "visual indicator" either to show what position the object is about to spawn at, or to play an animation on the prefab that is triggered when it first enters the scene.
The purpose of this is to give the player advanced notice and/or extra time to figure out where the object is spawning and get into position. I've tried to do a very simple animation but can't figure out how to get it to play when the prefab is instantiated.
A solution to the above, or any other suggestion that provides either a visual indicator of spawn position, or spawn animation of the prefab when instantiated, would be greatly appreciated!
Edit extra info of what I currently have tried
At the moment, I have an Animation Controller set up on the prefab, and Default animation has a transition to the Spawn Animation clip I created in Unity. There is a transition between the two with a trigger set up, the idea I currently have being to trigger this when the prefab is instantiated into the scene, so the animation plays when the prefab enters the scene.
My problem is, I have absolutely no idea what code will check the prefab has been instantiated into the scene, then set off the trigger so the animation will play. Maybe it's something really obvious that I'm missing, but to be pointed in the right direction would be very helpful.
Thanks all.
Show us what you've tried so we've got something to work from and we are not re-doing the same thing.
Of course.
At the moment, I have an Animation Controller set up on the prefab, and Default animation has a transition to the Spawn Animation clip I created in Unity. There is a transition between the two with a trigger set up, the idea I currently have being to trigger this when the prefab is instantiated into the scene, so the animation plays when the prefab enters the scene.
$$anonymous$$y problem is, I have absolutely no idea what code will check the prefab has been instantiated into the scene, then set off the trigger so the animation will play. $$anonymous$$aybe it's something really obvious that I'm missing, but to be pointed in the right direction would be very helpful.
Thanks :)
Answer by MXPServer20953OneTechGuy · Feb 12, 2021 at 02:17 AM
this is the way to do it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnsys : MonoBehaviour
{
public GameObject Object;
public float spawnT;
public float spawnD;
public bool stopSpawning = false;
AudioSource audioSource;
public AudioClip impact;
public ParticleSystem M;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("spawn", spawnT, spawnD);
}
// Update is called once per frame
void spawn()
{
Instantiate(Object, transform.position, transform.rotation);
M.Play();
if (stopSpawning)
{
CancelInvoke("spawn");
}
}
}