I'm making a game on unity where the user selects a character and the character spawns into the game world. The world consists of different doors. I want to add a transition animation (just a regular fade) between scenes in the game world but because the character is instantiated during runtime, I'm not sure how to attach the animator to the character. I also want the animation to trigger upon collision of the player with a door. I know how to create the animation clips and the animator but I need help on knowing when and how to attach the animator to an object that's going to be instantiated during runtime. Will I attach the animator in OnCollisionEnter() function? If so, how do I reference that animator through code?
Here is my code for OnCollisionEnter in a script that is attached to the player during runtime. (this works fine)
private void OnCollisionEnter(Collision collision)
{
GameObject door = collision.gameObject;
if (door.CompareTag("ExitDoor"))
SceneManager.LoadScene(0); // spawn back at main lobby
else if (door.CompareTag("RoomDoor"))
{
GameObject Room = door.transform.parent.gameObject;
if (Room.name.Equals("Room1Door"))
SceneManager.LoadScene(1); // go to first room
if (Room.name.Equals("Room2Door"))
SceneManager.LoadScene(2); // go to second room
if (Room.name.Equals("Room3Door"))
SceneManager.LoadScene(3); // go to third room
}
}
And here is the script of instantiating the player during runtime when the scene is loaded (this is in another script)
public GameObject InstantiatePlayer()
{
characterIndex = PlayerPrefs.GetInt(playerprefkey);
selectedChar = characters[characterIndex];
selectedChar.tag = "Player";
selectedChar.AddComponent<MoveRooms>(); //attaches the script where OnCollisionEnter is
return Instantiate(selectedChar, spawnPoint.transform.position, spawnPoint.transform.rotation);
}
to get the Animator, you can do this if the script is attached
Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
}
else if it is not attached you can try something like this
GameObject object;
Animator anim;
void Start()
{
object = GameObject.Find("objectsname");
anim = object.GetComponent<Animator>();
}
For further details how to use Animator
https://docs.unity3d.com/ScriptReference/Animator.html
https://docs.unity3d.com/ScriptReference/Animator.Play.html https://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html
As for a transition , you can try to use Caroutines. What Caroutines are is it executes a piece of code 'on the side' whie the other codes continue. It can be used to "wait" for a few seconds (or more). Its very useful and you can do alot with it.
some examplee
public class ExampleClass : MonoBehaviour
{
void Start()
{
// Start function as a coroutine.
StartCoroutine(waitForSeconds);
}
private IEnumerator waitForSeconds()
{
//do something
yield return new WaitForSeconds(1f); // the number is in seconds.
//do something else
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.