This script gets the Animator component attached to the character in the Start() method and then moves the character and plays the appropriate animation based on input in the Update() method. It also checks if the space key is pressed and plays the "jp" animation while adding a force to the character's rigidbody to jump. Make sure to adjust the force value to your liking.
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
private Animator anim; // Animator component attached to the character
private void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
// Get input axis for horizontal and vertical movement
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
// Move character based on input
transform.position += new Vector3(hAxis, 0, vAxis) * Time.deltaTime;
// Set animation based on input
if (hAxis != 0)
{
anim.Play("rl");
}
else if (vAxis != 0)
{
anim.Play("fb");
}
// Check if space key is pressed and jump
if (Input.GetKeyDown(KeyCode.Space))
{
anim.Play("jp");
// Add force to character's rigidbody to jump
Rigidbody rb = GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddForce(Vector3.up * 500f);
}
}
}
}