- Home /
The question is answered, right answer was accepted
Mecanim Transition-Arrow Pulsing
I just start learning how to work with Mecanim, I have figured out how to setup a generic characters, now I am trying to figure out the Animator.
A very basic state machine
I set the transition condition from Idle->Move to be [ speed > 0.1 ] and Move->Idle [ speed < 0.1 ].
In my test script:
using UnityEngine;
using System.Collections;
public class MecanimControl : MonoBehaviour {
private Animator animator;
void Start () {
animator = GetComponent<Animator>();
}
void Update () {
if( Input.GetKeyDown(KeyCode.A) ) {
animator.SetFloat("speed", 0.5f);
}
else if( Input.GetKeyUp( KeyCode.A ) ) {
animator.SetFloat("speed", 0f);
}
}
}
What happen now is: when I pressed A, the transition happens more than once. My character blend to Move, then snap back to Idle and blend back to Move again. I noticed that the transition arrow (the selected one, in blue) is showing a progress line, which means it is re-transit from Idle to Move again.
From my understanding in state machine, once I transit to the Move, my current state would be Move, therefore the transition condition [ speed > 0.1 ] for Idle To Move should not work. The only condition I will be abiding to when I am in the Move-state should be [ speed < 0.1 ].
Am I missing any settings or fundamental ideas on Mecanim? Please enlighten me in this issue.
[Edit #2] Blend Tree
sort of fix the moving problem by using a blend tree.
However, the transition problem still persist.
void Update () {
if( Input.GetKey(KeyCode.A) ) {
animator.SetFloat("speed", 1f, 1.5f, Time.deltaTime);
}
else {
animator.SetFloat("speed", 0f, 0.2f, Time.deltaTime);
}
if( Input.GetKeyDown(KeyCode.S) ) {
Attack();
}
}
When I pressed S, I set a bool to play an attack animation. However, since the bool stays true, it will keep on transit from idle -> attack over and over again.
Solution
by EliteMossy*
Do not connect AnyState to Idle (Period)
It solves my first problem with speed
.
For my attack-bool in the second part of my question, It will stay true and stuck at the attack-state. To fix that, I used the exit-time to transit back to idle, I also make sure that the attack-bool is set false a short while after it is set true, so that I will transit to attack-state whenever I want to.
I have Googled "mecanim transition once", "mecanim stay at state" without much result.
For my idle and walk, my setup is:
Transition Requirement:
Idle -> $$anonymous$$oving : [speed > 0.1]
$$anonymous$$oving -> Idle : [speed < 0.1]
Screenshot's not showing, what is the speed float (in the bottom left corner of animator) showing when it is transition from move > idle ? Because it seems weird.
Only thing i can think of is you have not set Loop Pose on the animation clip.
Answer by EliteMossy · Apr 05, 2013 at 01:00 PM
I fixed your issue, you had a transition between Any State and Idle state :D
Answer by Gurc · Apr 05, 2013 at 05:00 PM
I must say you use wrong function call. Input.GetKeyDown() is triggered once when you press button. Next frame bool is set to false, so does you speed. Use Input.GetKey(); I suppose the rest was right from the beginning.
I must say you use wrong function call. Input.Get$$anonymous$$eyDown() is triggered once when you press button. Next frame bool is set to false, so does you speed. Use Input.Get$$anonymous$$ey();
I neither agree nor disagree.
if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) ) {
animator.SetFloat("speed", 0.5f);
}
else if( Input.Get$$anonymous$$eyUp( $$anonymous$$eyCode.A ) ) {
animator.SetFloat("speed", 0f);
}
When I press A, speed will set to 0.5; even thought Input.Get$$anonymous$$eyDown()
will not return true in the next frame, speed will stay at 0.5 and will not change to 0 until I release A in Input.Get$$anonymous$$eyUp()
.
This will not create any re-transition from idle-state -> move-state, this code is not okay because it will produce jagged transition between animations because the speed
are set abruptly, not in a smooth lerp .
I suppose the rest was right from the beginning.
The only thing that is causing my problem is my setting/configuration mistake on the controller in the beginning. I should not connect AnyState to my Idle-state.