HI i have been working on a valve animation in my personal project.I added a triggered animation for the valve. but now as there are many valves with specifically the same animation and therefore the same animator.the animation is switched on for every object coupled with the particular game object…
Is there any way to animate only a specific object at an instance although many objects may share the same animator??(animate only the selected object(Valve) while other valves don’t animate unless they are triggered.)
Note: all the valves share the same animator controller as they have the same animation with the same parameter.
**Step 1: ** So this is fairly easy to do, say that you make one animation for the valves that rotates to simulate turning it.
_
**Step 2: ** Creating it with the animation tab; Unity should automatically create a animation controller, if not right click in Project to create new.
_
**Step 3: ** Put that animation in the animator as a state.
_
**Step 4: ** On your next valve (or object) add a animator component, add the animation controller from the before and done.
_
All valves/objects use the same animation.
Your conditions are why all of them become activated at once.
Write a C# script to determine which valve should turn, the only parameters you should set in the Animator are those that decide which animation (or animation state) to play.
Finally, as for detecting which valve should play the turning animation use something along these line:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ValveSelectorScript : MonoBehaviour
{
public GameObject[] valvesList; //put all your valves here from the inspector
List<Animator> animatorList = new List<Animator>();
void Start()
{
if (valvesList.Length >= 1) //make sure list isn't empty
{
for (int i = 0; i < valvesList.Length; i++) //NOTE: do "valvesList.Length - 1" instead, if you get index out of range error
{
animatorList.Add(valvesList*.GetComponent<Animator>()); //fill up your list with animators components from valve gameobjects*
animatorList*.enabled = false; //turn off each animator component at the start*
}
}
else
{
return; //if list is empty do nothing
}
}
public void FindValve(string valveName)
{
if (valvesList.Length >= 1)
{
for (int i = 0; i < valvesList.Length; i++) //NOTE: do “valvesList.Length - 1” instead, if you get index out of range error
{
if(valvesList*.name == valveName)*
{
//DO STUFF HERE
animatorList*.enabled = true;*
animatorList*.Set(//enter parameter here\); //set the animator parameter to play the animation*
//Remember to turn off this specific animator to avoid turning when another valve is activated. i = the number of the animator in the list. if in the inspector it says: “Element 0” then this would be the same as “animatorList[0]”
}
}
}
else
{
return;
}
}
void OnTriggerStay(Collider other)
{
if (keypress)//whatever you do
{
string valveGameobjectName = other.gameObject.name;
FindValve(valveGameobjectName);
}
}
void Update()
{
}
}
_
If you have any questions or need additional help, simply comment on this answer or my E-mail can be found on my profile.
I hope this helps, if you accept this answer please up vote it to help others facing the same issue find it easier.
-
RocketFriday