[https://www.youtube.com/playlist?list=PLyH-qXFkNSxmDU8ddeslEAtnXIDRLPd_V
I’m following along a tutorial to create a 2D platformer, and it’s designed for the 2022 version. However, I’m following along in Unity 6. I’ve gotten to the point of creating animations, specifically idle, walk, and run. I’ve created the states in the animator window, and set the transitions, but when I hit Play, I only ever get the idle animation, even while moving.
Sounds like you wrote a bug… and that means… time to start debugging!
First, start here:
Anything with Animations / Animators / Mechanim:
Only consider the code AFTER you have done this critical step:
Always start with the Animator state machine and prove it works in isolation, no code at all.
Here’s more reading:
If it doesn’t work, set it aside and recreate the animator. It’s only a few states, looks like six or so transitions, just rip them back out again, checking your work over as you go.
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
Two steps to tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody2D))] // Used to ensure that a rigidbody exists for the applicable 2d component
public class PlayerController : MonoBehaviour
{
public float walkSpeed = 5f;
Vector2 moveInput;
[SerializeField]
private bool _isMoving = false;
public bool IsMoving { get
{
return _isMoving;
}
private set
{
_isMoving = value;
animator.SetBool("isMoving", value);
}
}
[SerializeField]
private bool _isRunning = false;
public bool IsRunning {
get
{
return _isRunning;
}
set
{
_isRunning = value;
animator.SetBool("isRunning", value );
}
}
Rigidbody2D rb;
Animator animator;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
rb.linearVelocity = new Vector2(moveInput.x * walkSpeed, rb.linearVelocity.y);
}
public void OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
IsMoving = moveInput != Vector2.zero;
}
public void OnRun(InputAction.CallbackContext context)
{
if (context.started)
{
IsRunning = true;
} else if(context.canceled)
{
IsRunning = false;
}
}
}