Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html
Related
I am trying to start an animation by pressing a key on my keyboard.
For this I am using the following code:
[HideInInspector] public Animation animation;
private void Start()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
animation.Play("AnimationName");
}
}
This gives me the following error:
The animation state AnimationName could not be played because it
couldn't be found!
Although, the animation DOES start, but how can I clear this error?
I think the only possible explaination is that your animation clip is not marked as Legacy.
https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/
EDIT with step by step here:
Animations must be marked as legacy to be used with the animation component.
In the project window select the animation clips you’re trying to play.
Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode
Tick the “Legacy” checkbox and change the inspector back to normal mode.
Unity Inspector Animation Clip Legacy Checkbox
But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation.
Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use).
Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point).
It would be also easier to understand when it start and when is playing.
Another thing to check is the legacy 'Animation' component on your gameObject that is supposed to perform the animation. In my case things were marked as legacy for the animation on the imported animation but somehow when adjusting settings on my import it dropped the animation it was complaining about from the actual 'Animation' component so once I added it things worked as expected
Inspector in Debug mode showed my animation marked as 'Legacy' true
My Animation component showed the missing animation
I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:
I am using Unity and Vuforia, and would like to make an animation on AR object that starts when target is found and resets when target is lost so that, when target is found again the animation starts from the beginning.
In order to make animation start only once target is found, I selected the option "Cull Completely" in the Culling Mode property of the animator component. But I can't manage to reset the animation when target is lost ! I have tried modifying the DefaultTrackableEventHandler script (the OnTrackingFound and OnTrackingLost methods) but it doesn't work.
So far I have tried :
Animation[] animationComponents = GetComponentsInChildren<Animation>();
foreach (Animation component in animationComponents)
{
component.Stop();
}
and some variations (animation name as a parameter of Stop method, animator component instead of animation component, ...).
Does someone know how to do this ?
Thanks for your attention :)
I know a way of doing what you want in a simpler way. Look for https://docs.unity3d.com/Manual/class-State.html
You will get the same results but in a different way using the animation states.
Then go to the animator window
And you can build something like that. Those are transitions between states, associated to an animation.
Take a look at this too https://docs.unity3d.com/Manual/class-Transition.html
So in your script you can access to the Animator with:
Animator MyAnimator = GetComponent<Animator>();
And in somewhere over your conditions in the game, you can do this:
MyAnimator.Play("Attack");
If that animation doesn't have a transition, it will your decision to make it a loop animation (it will repeat when ends) or it will stay in the position of the last frame of the animation.
With some conditions you might want to have, like the one in your question, you could do something like this.
if (Vector3.Distance(this.transform.position, myTarget.transform.position) >= 2f) {
MyAnimator.Play("Idle");
}
Warning: That Vector3.Distance may not be the best and fastest way of doing it, it's just as an example. Remember to always try to optimize your code with your project requirements.
So I'm making a simple "puzzle" using lights. Simply, there are 3 buttons with lights on them (red, blue, green). Each button has it's own trigger volume but when I go to play, nothing prints that I even enter, stay, or leave the trigger. I've never used Collider Variables before so I feel like I'm doing something wrong (obviously or it would be working!). But then I just did "Collider entity" in the OnTriggerStay/Enter/Exit Method and it still didn't print to the console that my player was entering. Why are my Triggers not working?
Click here for the code I'm trying
Click here to see how I have it in the Unity Scene
Triggers only respond to other colliders that have rigid bodies on them.
Try adding a Ridgidbody component to your player and set it to kinematic.
OnTriggerEnter/Stay/Exit works when the Object has a Collider Component and BluePuzzle2 doesn't have that.
Also OnTrigger function gets a Collider as parameter. Check the reference page
So in order to make that work put a script on every light and on that script copy this function
void OnTriggerEnter(Collider col) {
if (col.CompareTag("Player")) {
print("Entered the trigger");
}
}
Hope it helps.
For my recent project i need a Door to open if the user is in front of it and has pressed "e".
This is the Code for the Button:
using UnityEngine;
public class Button : MonoBehaviour
{
public Animator Door; // In the editor, give a reference to your door. It must have an Animator script for this to work
void OnTriggerEnter(Collider c)
{
if (c.gameObject.tag == "Player")
{
//Text = "E to interact!"
if (Input.GetKeyDown("e"))
{
print("´Test");
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Door.SetTrigger("Open"); // The door's animator goes to "open" state
}
}
}
}
The Animator looks like this:
The Transition from Idle to Open:
The Button has the Door attached to it:
So i got two issues here, the first is - With the above Code nothing happens if i press E. No Error, No Action, whatsoever.
If i delete the Input.GetKeyDown("e") and make the Button-Mesh to a Trigger and run in it, it says
MissingComponentException: There is no 'Animator' attached to the
"Button" game object, but a script is trying to access it. You
probably need to add a Animator to the game object "Button". Or your
script needs to check if the component is attached before using it.
If you need more Information, just let me know. Thanks!
Ok, your approach to the interaction is wrong. First of all, On trigger enter will work only once. That is when you enter the trigger. So it won't take the Keypress event. You will have to move the code in OnTriggerStay(Collider col) where it will constantly be called. There if it takes any key event, then it will fire up. That will solve your problem 1.
As for the second problem, I'm pretty sure you copy paste this code from somewhere else. Only the canvas UI Button has animator state. Your button is Normal mesh and not the UI one. So setting the state button in that code won't work, ie
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Unless you are using the Canvas Button that script does not work. And the error description already mentioned the error you Button Mesh Does not have an animator. Try commenting the line and see how it works.
Edit:
I went through your transition your code should be
door.GetComponent<Animator>().SetTrigger("OnPress");
and not
GetComponent<Animator>().SetTrigger("OnPress");
As it is trying to call the OnPress state of door animator. If any trouble comes comment below with more info. Sorry did not notice this first.