Hey im trying to make a walking animation play when i press w,a,s,d (for a top down 2d game)
I have been looking around for a while and i could not fund anything.
Here’s my code (some of it is pointless because I did not delete the other ways I tried)
using UnityEngine;
using System.Collections;
public class PlayerController2D : MonoBehaviour
{
public int moveSpeed;
public int jumpHeight;
public int Crouch;
public Transform groundPoint;
public float radius;
public LayerMask groundMask;
public float speed;
public bool animation_bool;
Animator anim;
bool isGrounded;
Rigidbody2D rb2D;
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rb2D.velocity.y);
rb2D.velocity = moveDir;
isGrounded = Physics2D.OverlapCircle(groundPoint.position, radius, groundMask);
anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
if (Input.GetAxisRaw("Horizontal") == 1)
{
transform.localScale = new Vector3(1, 1, 1);
}
else if (Input.GetAxisRaw("Horizontal") == -1)
{
transform.localScale = new Vector3(-1, 1, 1);
}
GetComponent<ConstantForce>().enabled = false;
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb2D.AddForce(new Vector2(0, jumpHeight));
}
if (Input.GetKeyDown(KeyCode.W))
GetComponent<Animator>().SetBool("Walking up", true);
}
}