Home>

In Unity, I want to change the animation by pressing the → key,
I can't do well.
The setting of Unity is written as follows in visual studio as shown in the image.

] (60a0d5c36b075396b560d3cbb0339727.png)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement: MonoBehaviour
{
Animator animator;
void Start()
{
animator = GetComponent();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
if (x>0)
{
animator.SetFloat("speed",x);
}
else if(x == 0)
{
animator.SetFloat("speed", x);
}

}
}

=======================
Please tell me where to fix.
If i do not understand the situation, I will upload additional images, so please tell me the necessary information.

  • Answer # 1

    The following two are premised.

    PlayerAnimation = state with animation in idle state assigned to Motion

    RunAnimation = State with running animation assigned to Motion

    First, it's a good idea to lay out the Hierarchy, Scene, Game, and Animator windows so that you can see them at the same time.

    Leave the PlayerMovement component disabled.

    Run the scene and select the object that has the Animator component added to it to activate the Animator window. In this state, there should be an indication that "PlayerAnimation state is playing".

    Now, rewrite the Animator parameter "Speed" to 1. After that, the state will transition to RunAnimation, and the Animator window will show that as well.

    Then set Speed ​​back to 0. If Transition of RunAnimation → PlayerAnimation is set properly, the state will return to PlayerAnimation.

    Stop execution and enable the PlayerMovement component.

    Run the scene and press →. If the PlayerMovement and Animator components were attached to the same object, the Speed ​​parameter would now increase and a State Transition would occur.

    If not, please tell me at what step you were in a different situation than what was written and what was different.

    By the way, Transition's Has Exit Time is checked, but this means that the transition before the animation before the transition is played to the end is completed, so in this case you should uncheck it.