Unity 2D Animation Won't Play after SetActive(true) - c#

I have a 2D single screen style game where I have a disabled gameobject. Under this are several children that that are animated. The parent object holds the animator for the children. When I enabled the parent, the animations will not play. The animator looks as thought it's trying to processes the default entry state but does not complete. I've done some research on this and it seems that disabling and then enabling something does not start the animator. I've tried writing some code for it with no luck and setting the sprite renderer false won't work either as there are simply too many sprites. Here's my last attempt: Any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animationState : MonoBehaviour
{
public Animator anim;
private void OnEnable()
{
if (anim.gameObject.activeSelf)
{
anim.Play("pinWheelAnimation");
}
}
}

Unity doesn't work like that, You can't set an animator to the parent object that is going to influence animations from children, you are going to have to set up an animator for every child. If you want to modify animators of children from a parent object you can do that with a script that searches for all children and their components and changes their parameters.
Take a look at this: Animator parameters

Try to set an IdleState as DefaultState with no exit time and no animation and define a trigger event to go over to your animation state. Then activate the trigger in
void Start()
{
}

Related

cant play an animation when pressing a button

screen shot of the animator panel
so as you can see I am trying to play an animation called Attack when triggering the parameter Attack but whenever I press the triggering button that I chose the animation just doesn't work and the trigger does not get activated
here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attack : MonoBehaviour
{
public Animator animator;
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
Attack();
}
}
void Attack()
{
animator.SetTrigger("Attack");
}
}
by the way, I am trying to follow this guy's video
https://youtu.be/sPiVz1k-fEs
Suggestions:
Make sure you have assigned this script to a game object.
Assign the right animator that you want to animate in inspector of this script.
Check if you have added 'Attack' parameter in that animator and the spelling is same like the one mentioned as parameter in your code.
and lastly check if you have set the trigger for the transition of animation in the animator( in animator its the arrow that is pointing towards your animation ).
Let me know if you don't understand any of this I'll breifly explain.
Hopefully that will help... Happy coding :)

Animate an object by pressing Key / or Mouse Click Unity3D

currently i'm trying to animate an object by clicking or pressing a key. Unfortunately, script won't work and I have tried soooooo many other ways to do it.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnBoard : MonoBehaviour
{
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerStay(Collider player) {
if (player.tag == "Player" && Input.GetKeyDown(KeyCode.G)){
//Debug.Log("touchyy");
anim.SetTrigger("turn");
}
}}
I have assigned this script file to object itself. Object has a box collider. Animation has a trigger named "turn". When player enters to the collider zone, I want player to be able to activate animation of the object with a click/or keypress.
I do get "Debug.Log" when player enters the zone. So I believe that there is no problem in detecting the collision. But just can't manage to animate object in any way.
Any help? Thank you!
OnTriggerStay is run like FixedUpdate (since it involves physics), so none of the Input events will work correctly inside it for the same reason that they won't work right in FixedUpdate. All Input functions must only be used in Update method.

How to disable GameObject after animation is finished

So I have an animation that fades into the menu screen but after the animation ends none of my buttons work. I have figured out that it is because the GameObject that holds the black image that fades to clear is always in the front, blocking me from using any of the buttons. I tried to write a script, that's attached to the game object, that disables the GameObject after it completes the animation, but it isn't working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelChanger : MonoBehaviour
{
public Animation anim;
public void SetTrigger()
{
this.StartCoroutine(this.PerformAnimRoutine());
}
private IEnumerator PerformAnimRoutine()
{
var state = anim.PlayQueued("Fade_In", QueueMode.PlayNow, PlayMode.StopSameLayer);
yield return new WaitForSeconds(state.length);
this.gameObject.SetActive(false);
}
}
Is there anything wrong with the code or is there an easier way to accomplish this? I am extremely new to unity so I am very stuck.
If all you are doing is fading a sprite to clear and you seem to know about coroutines, I might start by suggesting you do the fade within a coroutine instead.
Have that decrease the alpha by some fraction each frame and when it's 0 disable the object.
That's just if that sounds more fluid, nothing wrong with the animation way.
Doing it with animations though :
I'm not confident that you can disable the object the animation is on in that animation. If it is available on the dope sheet try that. Otherwise we can use state behaviours or animation events.
Animation Events
These can be used to trigger a function at a certain point of an animation. You can create them similar to keyframes. Here is a link to Unity's guide on this topic.
All you'd need to do is create an event and place it at the end of the animation. Then you need to in a script of that object make a public function that simply disables the object. Call that with the event.
State Behaviours
State Machine Behaviours allow you to define a script to run on a given animation state. It has many functions to hook onto such as OnStateEnter and OnStateExit.
You'd want to click on the state that fades in the animator. In the inspector you should be able to click "Add Behaviour". This will create a script that you can open and edit. Here is the reference for that class.
From there is should be very simple to disable the object through OnStateExit.

Enable/ Control script on game object from animator controller in unity?

I want to create an analog glitch effect in unity for may main camera when my game character/ camera looks at a certain object.
I have seen it in this youtube video, but the part I am struggling with was not explained.
I am using this Glitch .unitypackage from Github (link).
I attached the AnalogGlitch Script from the package to my main camera game object, but this will apply the glitch effect the entire time.
Instead I want to enable the glitch effect when my character/ camera faces this certain object. I have a working animator controller, which enters a "facing state" when my camera looks at the object and a "Not facing state" when looking somewhere else (as seen in the next image).
Ideally I would just enable/ disable the AnalogGlitch.cs Script from my main camera game objects when this animation starts. I tried setting up a EnableGlitch.cs script for this, but it will not let me reference any game object in my scene. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableGlitch : StateMachineBehaviour
{
public GameObject maincamera;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
//maincamera.GetComponent<AnalogGlitch>().enabled = true;
}
}
Also is there a better way to control the AnalogGlitch.cs script from the animator controller? I would also like to control the variables within this script to modify the effect while in this animation state.

I would like two objects to play their separate animations when the user clicks on them in Unity using C#

I am new to coding, so please go easy and try to make this as simple as possible. I am surprised that what I am trying to do appears to be so difficult.
I have two objects, A and B. They each have a different animation, 1 and 2. I want the user to click on A and watch animation 1, or to click on B and watch animation 2. I also want each object to play their relevant animations and then change to different scenes.
Can this be done just in the Animator window without any code using bools or triggers and transitions? (I have tried multiple ways and can't work it out.)
If it needs a code attached to objects A and B, I have a trigger animation script (attached below) but it only plays one of the animations and is just if the mouse button is clicked, not by clicking on object A or B.
I can successfully do what I need with a change scene script: objects A and B have the change scene script attached and two different scenes to change to. How is the animation I want to play different and seemingly so complicated to implement? I haven't needed to declare any gameobjects in that script. Perhaps I need to GetComponent and something to do with PlayAnimation, but I have searched and searched and am surprised no one else has had this issue, or perhaps I'm not asking in the correct way. There are many tutorials that tell you how to play one animation, but not multiple.
using UnityEngine;
using System.Collections;
/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The name of the Animator trigger parameter")]
public string triggerName;
void Update()
{
// If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
if (Input.GetMouseButtonDown(0))
{
animator.SetTrigger(triggerName);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour
{
public void GoToScene(string sceneName)
{
Debug.Log("I'm going to change scenes!");
SceneManager.LoadScene(sceneName);
}
}
Thank you so much in advance. I'm really stuck. I am trying to learn by following tutorials and using Sololearn, but I'm finding it very difficult to translate what I'm learning into things I want to do.
Here's the updated code:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
public Animator animatorVR;
public Animator animatorAR;
public void OnPointerClick(PointerEventData pointerEventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
void Update()
{
// If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
//OnPointerClick;
{
animatorVR.SetTrigger(onPressVR);
animatorAR.SetTrigger(onPressAR);
}
}
}
I know this doesn't work but am not sure where to go from here.
Detecting a click on an object
Input.GetMouseButtonDown only checks the current state of the button, without any respect as to where the mouse is pointing. You're executing it within the Update event method, which runs every single frame.
The easiest way to detect if you're clicking an object is to put a Collider component (of any shape) on the object and use the OnMouseDown event function.
As noted in the comments by Galandil, OnMouse methods shouldn't be used for touch controls.
Running animations for two different objects
The Animator component can control a tree of objects (the object with the component, but also its children). You can do what you want without any additional code if you put objects A and B under a common parent object with Animator. Otherwise, yeah, you'll have to reference the other object somehow, e.g put something like this on both objects:
[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The Animator component on the other gameobject")]
public Animator otherAnimator;
...
animator.SetTrigger(triggerName);
otherAnimator.SetTrigger(triggerName);

Categories