- Home /
The question is answered, right answer was accepted
Multiple Game Objects with single Animator?
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.
Hi @HaranStaubitz - Your question is bit vague, also you might consider editing your question, it is pasted twice in row, and there is a strange link in "object is" words.
"is there any way to animate only a specific object at an instance although many objects may share the same animator" - I don't quite get what you mean by this?
hi @eses I'm sorry for that bad articulation of a question .I've edited the question as clear as i could, the simple way to say it is, "$$anonymous$$ultiple objects have the same animator so if a trigger animation is used by using a Collider, all the objects animate simultaneously rather than the triggered object only".
the default is to animate only one object. you must be calling all of them. how do you activate a valve to play it's animation?
I understood your problem. You can achieve this by using animation states. You just have to make different states for all the valves and then you can use the trigger to start/stop the animation.
@Piyush_Pandey So i have to like make a state for every valve?? which seems to be a round about way to like add new animator for each object, isn't it?
@hexagonius i animate using a trigger parameter to the animation and when the Character comes near the game objects' Collider, and presses a hot key the trigger is on. So in my case there are many such valves doing the same thing so if i trigger one valve all the valves get animated.
NO. Animator is a component while animation states are in an animator controller which is a parameter in an Animator controller. How is it better? ---because the Unity engine will have to handle lesser number of components.
Answer by RocketFriday · Aug 24, 2018 at 10:09 AM
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 in Unity should automatically create a animation controller, if not right clip 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[i].GetComponent<Animator>()); //fill up your list with animators components from valve gameobjects
animatorList[i].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[i].name == valveName)
{
//DO STUFF HERE
animatorList[i].enabled = true;
animatorList[i].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
@RocketFriday Thank you so much... i was about to do the same thing, and I saw your Answer and checked it out in a separate scene. it works the way i want, now all i have to do is incorporate it in my project. Thank You So much man and this was my first Question and i get a great answer like this. thank you so much really!!
For me it works not right. The animation only works correctly for the first object for which I created this animation, but I added this animation to other objects, and during animation they just move to the position of the first object. When I turn on the root movement for animation, all objects move infinitely in a strange direction.
Thanks! Also this 'https://answers.unity.com/questions/356442/using-the-same-animation-on-duplicate-objects.html' link could be useful.
Follow this Question
Related Questions
How to handle multiple animation variants 1 Answer
Display additional animation on top of other animations 0 Answers
Multiple Animation Controllers 1 Answer
My Animator is acting Strange 1 Answer