I'm currently trying to figure out two things for my character's movement and animation.
- A better/faster way to track when movement stops than the IsSleeping method.
- 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");
}
}
ani.SetBool
toani.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 replacingInput.GetAxis
withInput.GetAxisRaw
. \$\endgroup\$