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
3.2k views
in Technique[技术] by (71.8m points)

c# - 2D Animation stuck on jumping animation

I was watching an old brackeys video about how to do 2d animation and I'm confident I've followed everything in the tutorial properly. Everything was going fine until I started the jump animation. The jump animation will not stop during the on landing function. The video is here https://www.youtube.com/watch?v=hkaysu1Z-N8&feature=emb_logo and my code is this:

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

public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public float runSpeed = 80f;
float horiztalMove = 0f;
bool jump = false;
public Animator animator;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{

    horiztalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

    animator.SetFloat("Speed", Mathf.Abs(horiztalMove));

    if (Input.GetButtonDown("Jump"))
        jump = true;
        animator.SetBool("Jumping", true);
}
 public void OnLanding ()
{
    animator.SetBool("Jumping", false);   
}

void FixedUpdate()
{        //move our character
    controller.Move(horiztalMove * Time.fixedDeltaTime, false, jump);
    jump = false;
}

} If there is any other code or screenshots you would like me to send i will.


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

1 Answer

0 votes
by (71.8m points)

In this line, you have not added the "{}".

if (Input.GetButtonDown("Jump"))
        jump = true;
        animator.SetBool("Jumping", true);

Write this instead:

if (Input.GetButtonDown("Jump")
   {
       jump = true;
       animator.SetBool("Jumping", true);
   }

And I have seen the Brackeys Video that you have used... And my question is, have you added the PlayerMovement script to the CharacterController's On Land Event and selected OnLanding() function that you've made in the PlayerMovement script?

Try checking that you've added the OnLanding() and also fix the {} in the line that I've pointed out. Hope I turn out be helpful to you. <3


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

...