Forum begins after the advertisement:


[Part 1] Problem with animation transitions + serialized field errors

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] Problem with animation transitions + serialized field errors

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #18916
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Reposting a YouTube comment by @HeavenlyZanpakuto:

    I’m having trouble with the transitions between animations. Also when I try to flip my character’s direction I end up with them becoming flat and hard to see. Here is my code. Can anyone help? I need this tutorial, I’m making a project for school that I need to have done & I want to add my own sprites on top of the template ones I’m using.

    Player Controller Script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings")]
        [SerializeField] private float walkspeed = 10;
    
        [Header("Ground Check Settings")]
        [SerializeField] private float jumpForce = 45f;
        [SerializeField] private Transform groundcheckPoint;
        [SerializeField] private float groundCheckY = 0.2f;
        [SerializeField] private float groundCheckX = 0.5f;
        [SerializeField] private LayerMask whatIsGround;
    
    
        private Rigidbody2D rb;
        private float xAxis;
        Animator anim;
    
    
        public static PlayerController Instance;
    
        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
        }
    
    
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
    
            anim = GetComponent<Animator>();
        }
    
        // Update is called once per frame
        void Update()
        {
            GetInputs();
            Move();
            Jump();
            Flip();
    
            if (Input.GetButtonDown("Jump"))
            {
                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            }
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        void Flip()
        {
            if(xAxis < 0)
            {
                transform.localScale = new Vector2(-1, transform.localScale.y);
            }
            else if(xAxis > 0)
            {
                transform.localScale = new Vector2(1, transform.localScale.y);
            }
        }
    
        private void Move()
        {
            rb.velocity = new Vector2(walkspeed * xAxis, rb.velocity.y);
            anim.SetBool("walking", rb.velocity.x != 0 && Grounded());
        }
    
        public bool Grounded()
        {
            if (Physics2D.Raycast(groundcheckPoint.position, Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundcheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
                || Physics2D.Raycast(groundcheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
                || Physics2D.Raycast(groundcheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        void Jump()
        {
            if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
            {
                rb.velocity = new Vector2(rb.velocity.x, 0);
            }
    
            if (Input.GetButtonDown("Jump") && Grounded())
            {
                rb.velocity = new Vector3(rb.velocity.x, jumpForce);
            }
    
            anim.SetBool("Jumping", !Grounded());
        }
    } 

    Also for the camera script the serialized field at the beginning seems to be a compiler error, though I don’t know why.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CameraFollowScript : MonoBehaviour
    {
        [serializedField] private float followSpeed = 0.1f;
    
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
            transform.position = Vector3.Lerp(transform.position, PlayerController.Instance.transform.position, followSpeed);
        }
    }
    #18917
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Hey Terence! Let’s tackle your issues one by one.

    1. Animation Transitions

    For the transitions between animations, make sure that you have set up your Animator Controller correctly. You should have parameters (like “walking” and “Jumping”) that control the transitions between different animation states. Ensure that:

    • You have transitions set up between your idle, walking, and jumping animations.
    • The conditions for these transitions are based on the parameters you set in your script (like anim.SetBool("walking", ...) and anim.SetBool("Jumping", ...)).

    2. Character Flipping Issue

    The issue where your character becomes flat when flipping is likely due to how you’re scaling the character. In your Flip() method, you’re changing the x-scale of the character’s transform. If your character’s sprite is not set up correctly (e.g., if the pivot point is not centered), it might appear flat when flipped.

    Make sure your sprite’s pivot is set to the center in your sprite editor. Additionally, you might want to check the import settings of your sprite to ensure it’s not being compressed or altered in a way that affects its appearance.

    3. Camera Script Compiler Error

    The error in your camera script is due to a typo in the attribute name. It should be [SerializeField] (with a capital “S”). Here’s the corrected line:

    [SerializeField] private float followSpeed = 0.1f;

    Final Suggestions

    • Ensure that your animations are set up correctly in the Animator and that the transitions are based on the parameters you are modifying in your script.
    • Check your sprite settings to avoid the flattening issue when flipping.
    • Fix the typo in your camera script.

    If you make these adjustments and still face issues, feel free to share more details, and I’ll be happy to help! Good luck with your project! 🎮

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: