Home > Blockchain >  Unity2d: the player stop moving when I add an animation
Unity2d: the player stop moving when I add an animation

Time:01-12

enter image description here

I have a problem in unity , the player mouvement is going good until I add an animation the player stop moving even if I press the keyboard keys,when I remove the animator compenent the player move normaly without problems !

I tried separate the animation script from the movement script and still theame problem , I don't think that the problem is comming from the code playerAnimation code :

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

public class playerAnim : MonoBehaviour
{
    Animator anim;
    Rigidbody2D rb;
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
    }
    void FixedUpdate()
    {
        if (rb.velocity.x == 0)
            anim.SetBool("isRunning", false);
        else
            anim.SetBool("isRunning", true);
        if (rb.velocity.y > 0)
            anim.SetBool("isJumping", true);
        else
            anim.SetBool("isJumping", false);
    }
}

playerMovement script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class playerControl : MonoBehaviour
    {
        Rigidbody2D rb;
        private float horizontal;
    
        public float runSpeed;
        public float jumpPower;
        public  bool inTheGround;
        private SpriteRenderer sp;
        Animator anim;
    
        // Start is called before the first frame update
        void Start()
        {
            rb = gameObject.GetComponent<Rigidbody2D>();
            sp = gameObject.GetComponent<SpriteRenderer>();
            anim = gameObject.GetComponent<Animator>();
        }
        private void Update()
        {
            horizontal = Input.GetAxisRaw("Horizontal");
    
        }
        private void FixedUpdate()
        {
            rb.velocity = new Vector2(horizontal * runSpeed, 
            rb.velocity.y);
         
        
    
            if (Input.GetButton("Jump")&& inTheGround)
            {
                
                rb.velocity = new Vector2(rb.velocity.x,jumpPower);
            }
            flipping();
            Debug.Log(rb.velocity.x);
    
        }
        private void OnCollisionEnter2D(Collision2D other)
        {
            if (other.gameObject.CompareTag("ground"))
                inTheGround = true;
        }
        private void OnCollisionExit2D(Collision2D other)
        {
            if 
           (other.gameObject.CompareTag("ground")&&rb.velocity.y>0.1)
                inTheGround = false;
        }
        void flipping()
        {
            if (Input.GetKey(KeyCode.RightArrow))
                sp.flipX = false;
            if (Input.GetKey(KeyCode.LeftArrow))
                sp.flipX = true;
        }
       }

CodePudding user response:

Check if Apply Root Motion on Animator Component is set to false. This setting can overwrite your changes of the object's position over time. if not - can you please provide more information, perfectly a screenshot of your player components, and Animator Controller.

  • Related