Search Unity

Question No Animation by moving

Discussion in 'Animation' started by DerWaldFried, Mar 11, 2023.

  1. DerWaldFried

    DerWaldFried

    Joined:
    Dec 18, 2022
    Posts:
    5
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public float jumpForce = 10f;
    9.     public LayerMask groundLayer;
    10.  
    11.     private Rigidbody2D rb;
    12.  
    13.     private void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetMouseButtonDown(0))
    21.         {
    22.             MoveLeft();
    23.         }
    24.         else if (Input.GetMouseButtonDown(1))
    25.         {
    26.             MoveRight();
    27.         }
    28.         else if (Input.GetMouseButtonDown(2))
    29.         {
    30.             Jump();
    31.         }
    32.     }
    33.  
    34.     private void FixedUpdate()
    35.     {
    36.         // Check if the player is on the ground
    37.         bool isGrounded = Physics2D.OverlapCircle(transform.position, 0.2f, groundLayer);
    38.  
    39.     }
    40.  
    41.     private void MoveLeft()
    42.     {
    43.         rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
    44.         transform.localScale = new Vector2(-1f, 1f);
    45.         GetComponent<Animation>().Play("moveleft");
    46.     }
    47.  
    48.     private void MoveRight()
    49.     {
    50.         rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
    51.         transform.localScale = new Vector2(1f, 1f);
    52.         GetComponent<Animation>().Play("moveright");
    53.     }
    54.  
    55.     private void Jump()
    56.     {
    57.         if (Physics2D.OverlapCircle(transform.position, 0.2f, groundLayer))
    58.         {
    59.             rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    60.         }
    61.     }
    62. }
    63.  
    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:
    Bild_2023-03-11_172137814.png

    Anybody a Idea what i do wrong?

    Best Regards
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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
     
  3. DerWaldFried

    DerWaldFried

    Joined:
    Dec 18, 2022
    Posts:
    5
    Oh okay. Thanks. I will show the Video and try to Work with the new System :)
     
  4. DerWaldFried

    DerWaldFried

    Joined:
    Dec 18, 2022
    Posts:
    5
    Solved! Thank you for the help. I will have to understand the system a little more now, but my basic problem has been solved
     
    Unrighteouss likes this.