0
\$\begingroup\$

I'm currently trying to figure out two things for my character's movement and animation.

  1. A better/faster way to track when movement stops than the IsSleeping method.
  2. A way to track which direction my character is facing to send this information to the animator.

Here is the code I'm currently using for movement and sending information to the animator:

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

public class KittenController2 : MonoBehaviour
{
    Rigidbody2D rb;
    Animator ani;

    public float speed;
    public float jumpForce;

    private float direction = 0f;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        ani = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        direction = Input.GetAxis("Horizontal");
        ani.SetBool("Stationary", rb.IsSleeping());
        
        if (direction > 0f)
        {
            rb.velocity = new Vector2(direction * speed, rb.velocity.y);
            ani.SetFloat("Move X", direction);
        }
        else if (direction < 0f)
        {
            rb.velocity = new Vector2(direction * speed, rb.velocity.y);
            ani.SetFloat("Move X", direction);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }
    }

    void FixedUpdate()
    {

    }

    void Jump()
    {
        rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
        ani.SetTrigger("Jump");
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Presumably you considered checking whether the velocity is (nearly) zero, and whether the last non-zero x value it had was positive or negative? What do you find unsatisfying about the methods you're using so far, that answers can focus on improving? \$\endgroup\$
    – DMGregory
    Commented Nov 3, 2021 at 3:24
  • \$\begingroup\$ Hey! I'm really new to Unity, this is my first project outside of one of the unity learn tutorials. How would I go about checking the velocity in this way? As for the things I find unsatisfying, "IsSleeping" seems a little slow to update for smooth animation transitions, as the X Axis slowly returns to ~0 instead of stopping immediately. I can send a gif of it if you think that would be helpful. \$\endgroup\$
    – EvelynSays
    Commented Nov 3, 2021 at 3:39
  • \$\begingroup\$ You can try to modify ani.SetBool to ani.SetBool("Stationary", direction == 0.0f);. This may make the animator to reacts a bit faster. Another thing you can try, make input "instantaneous" (but less fluid) by replacing Input.GetAxis with Input.GetAxisRaw. \$\endgroup\$
    – Nikaas
    Commented Nov 3, 2021 at 8:01

1 Answer 1

1
\$\begingroup\$

Thanks for the input from everyone here. I managed to find a good solution to this that updates almost instantly.

Before any of the functions:

public float stationaryThreshold;
private float squaredStationaryThreshold;

Inside functions:

void Awake()
    {
        squaredStationaryThreshold = stationaryThreshold * stationaryThreshold;
    }

void Update()
    {
        ani.SetBool("Stationary", rb.velocity.sqrMagnitude <= squaredStationaryThreshold);
    }

This checks whether the square root of the movement velocity's magnitude is less than or equal to the square root of whatever the stationary threshold is set to.

I then have this communicating with the animator to achieve my goal, changing the animation from moving to idle as soon as the character stops moving.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .