- Home /
Question by
Roura123 · Sep 26, 2020 at 01:11 PM ·
animatoranimator controllerinstatiate
Instantiating animator controller and then using it in runtimeAnimatorController
Hi, I'm trying to instantiate animator controller prefab and then set it as the new animator controller with runtimeAnimatorController. The reason I'm doing this is that I'm trying to make a chest prefab and I need to separe the animators or they open all at once. Code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interacting : MonoBehaviour
{
public RuntimeAnimatorController prefabanim;
private GameObject pgm;
private Rigidbody prb;
private bool inter;
public Animator anim;
private void Start()
{
PrbInst();
AnimInst();
}
void Update()
{
Check();
Interact();
}
void Check()
{
RaycastHit hit;
if (Physics.Raycast(prb.transform.position, prb.transform.forward, out hit, 3f))
{
if (hit.transform.gameObject.CompareTag("Interactable"))
{
//Debug.Log("Seen");
inter = true;
}
}
else
{
//Debug.Log("XSeen");
inter = false;
}
}
void Interact()
{
if(Input.GetKeyDown(KeyCode.E) & inter == true & anim.GetBool("Interact") == false)
{
anim.SetBool("Interact", true);
}
else if (Input.GetKeyDown(KeyCode.E) & inter == true & anim.GetBool("Interact") == true)
{
anim.SetBool("Interact", false);
}
}
void AnimInst()
{
anim.runtimeAnimatorController = Instantiate(prefabanim);
Debug.Log(prefabanim.name);
}
void PrbInst()
{
pgm = GameObject.Find("Player");
prb = pgm.GetComponent<Rigidbody>();
}
}
The problem with this is that even though it doesn't have any errors it still opens all the chest at once.
Comment
Your answer
Follow this Question
Related Questions
How to handle multiple animation variants 1 Answer
Animator Layer executes when weight equals zero 1 Answer
Set Animation Parameter on only one of many Prefab References in Editor 0 Answers
Animation frozen 1 Answer
Trigger Animation Via Script 2 Answers