No Animation by moving

Hello lovely Community,

to Time i try me in a first GameJam, to learn more. Now i have a little Problem. Well:

My Player move left by press left mousebutton, move right by press right mousebutton and jump by pressing middle mousebutton. Now i want Animate the Movement, when the Player move. This is my Code:

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

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 10f;
    public LayerMask groundLayer;

    private Rigidbody2D rb;

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

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            MoveLeft();
        }
        else if (Input.GetMouseButtonDown(1))
        {
            MoveRight();
        }
        else if (Input.GetMouseButtonDown(2))
        {
            Jump();
        }
    }

    private void FixedUpdate()
    {
        // Check if the player is on the ground
        bool isGrounded = Physics2D.OverlapCircle(transform.position, 0.2f, groundLayer);

    }

    private void MoveLeft()
    {
        rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
        transform.localScale = new Vector2(-1f, 1f);
        GetComponent<Animation>().Play("moveleft");
    }

    private void MoveRight()
    {
        rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
        transform.localScale = new Vector2(1f, 1f);
        GetComponent<Animation>().Play("moveright");
    }

    private void Jump()
    {
        if (Physics2D.OverlapCircle(transform.position, 0.2f, groundLayer))
        {
            rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        }
    }
}

How you can see, i try to start on the Component “Animation” my Animations “moveleft” and “moveright”. But when i test it, it doenst play the animation. Here are the Component:
8869026--1210806--Bild_2023-03-11_172137814.png

Anybody a Idea what i do wrong?

Best Regards

The “Animation” component isn’t used anymore, it was replaced by the newer “Animator” component.

Here’s a video going over how to use the new system:

Once you add the Animator component, you need to create an Animator Controller asset in the project window: Right click → Create → Animator Controller

Assign the newly created Animator Controller to the empty field in the Animator component, then navigate to the Animator window to begin working: Unity’s top bar → Window → Animation → Animator

Oh okay. Thanks. I will show the Video and try to Work with the new System :slight_smile:

Solved! Thank you for the help. I will have to understand the system a little more now, but my basic problem has been solved

1 Like