Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I can't animate my player

Discussion in 'Animation' started by Turtle11t, Apr 14, 2023.

  1. Turtle11t

    Turtle11t

    Joined:
    Mar 25, 2023
    Posts:
    9
    Hi,

    I'm pretty sure someone else beat me to this, but I can't seem to animate my player for some reason (see screenshot)

    It doesn't seem to let me drag a set of frames into the animation window. A guide I'm following said to do so.
    I used the Free Pixel Army from the Unity Asset Store. I don't know what I'm doing wrong. Is it the script? The animator? The sprite?

    I'm using Unity 2022.2.14

    Thanks.
     

    Attached Files:

  2. CoolBeanz7

    CoolBeanz7

    Joined:
    Mar 14, 2022
    Posts:
    18
    The state controller looks fine at a glance, but I would need to see what variables are in the state controller and how they are applied to your transitions. It would also help to see your script.
     
  3. Turtle11t

    Turtle11t

    Joined:
    Mar 25, 2023
    Posts:
    9
    What do you mean by that?

    I hardly have anything for the animation, other than
    Animator animator;


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. // Takes and handles the movement for a player character
    7. public class Player1Controller : MonoBehaviour
    8. {
    9.    
    10.     public float moveSpeed = 1f;
    11.     public float collisionOffset = 0.5f;
    12.     public ContactFilter2D movementFilter;
    13.    
    14.    
    15.     Vector2 movementInput;
    16.     SpriteRenderer spriteRenderer;
    17.     Animator animator;
    18.     Rigidbody2D rb;
    19.     List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
    20.    
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         rb = GetComponent<Rigidbody2D>();
    25.         animator = GetComponent<Animator>();
    26.         spriteRenderer = GetComponent<SpriteRenderer>();
    27.     }
    28.    
    29.     private void FixedUpdate() {
    30.         //If movement input is not 0, try to move
    31.         if(movementInput != Vector2.zero){
    32.            
    33.             bool success = TryMove(movementInput);
    34.  
    35.             if(!success) {
    36.                 success = TryMove(new Vector2(movementInput.x, 0));
    37.  
    38.                 if(!success) {
    39.                     success = TryMove(new Vector2(movementInput.y, 0));
    40.                 }
    41.             }
    42.         }
    43.  
    44.         //Set direction of sprite to movement direction
    45.         if(movementInput.x < 0) {
    46.             spriteRenderer.flipX = true;
    47.         } else if(movementInput.x > 0) {
    48.             spriteRenderer.flipX = false;
    49.         }
    50.     }
    51.    
    52.     private bool TryMove(Vector2 direction) {
    53.         // Check for potential collisions
    54.             int count = rb.Cast(
    55.                 movementInput, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
    56.                 movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
    57.                 castCollisions, // List of collisions to store the found collisions into after the Cast is finished
    58.                 moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset
    59.            
    60.             if(count == 0){
    61.                 rb.MovePosition(rb.position + movementInput * moveSpeed * Time.fixedDeltaTime);
    62.                 return true;
    63.             } else {
    64.                 return false;
    65.             }
    66.     }
    67.  
    68.     void OnMove(InputValue movementValue) {
    69.         movementInput = movementValue.Get<Vector2>();
    70.     }
    71. }
    72.