Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

c# - Character Animation - Unity2D

I have this really weird problem and I don't know how to solve it, hope you guys can help me.

using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Charakter1 : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator anim;
    private float dirX;
    private float moveSpeed;
    private bool facingRight = true;
    private Vector3 localScale;

    // Use this for initialization
    private void Start()
    {
        localScale = transform.localScale;
        moveSpeed = 5f;
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    private void Update()
    {
        dirX = CrossPlatformInputManager.GetAxis("Horizontal") * moveSpeed;

        if (CrossPlatformInputManager.GetButtonDown("Jump")&& rb.velocity.y == 0)
        rb.AddForce(Vector2.up * 700f);

        if(Mathf.Abs(dirX) > 0 && rb.velocity.y == 0  )
        anim.SetBool("isRunning", true);
        
        else 
        anim.SetBool("isRunning", false); 

         if( rb.velocity.y == 0)
         {
             anim.SetBool("isJumping", false); 
             anim.SetBool("isFalling", false); 
         }


         if( rb.velocity.y > 0)
         
             anim.SetBool("isJumping", true); 
             
         

            if( rb.velocity.y < 0)
         {
             anim.SetBool("isJumping", false); 
             anim.SetBool("isFalling", true); 
         }
        

    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(dirX , rb.velocity.y);
    }

    private void LateUpdate()
    {
        CheckWhereToFace();
    }

    private void CheckWhereToFace()
    {
        if (dirX > 0)
            facingRight = true;
        else if (dirX < 0)
            facingRight = false;

        if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
            localScale.x *= -1;

        transform.localScale = localScale;
    }

}

Animator Window

The isJumping and isFalling Animations are keep getting triggered. The character then makes some really weird moves once I press the "Left" Button. This all doesn't happen when he walks to the right but only the left. When he should be doing isIdle, it just animates all 4 like I said above when he's standing still after he moved to the left. The Platform he's standing on is 100% straight

question from:https://stackoverflow.com/questions/65645049/character-animation-unity2d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...