简体   繁体   中英

How can I stop my character from jumping infinitely jumping in my script?

This is the movement script that was written to control the movement of my character. It depends on the Brackeys 2d controller script which is on his github, so you should be able to look at it if that is where the problem is but because I believe the problem is somewhere in this script.

I decided to show this one. My character can jump repeatedly and I don't know what the problem is so I would appreciate any help.

public CharacterController2D controller;
 public Animator animator;
 public float runSpeed = 40f;
 float horizontalMove = 0f;
 bool jump = false;

 void Update()
 {
     horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
     animator.SetFloat("speed", Mathf.Abs(horizontalMove));
     
     if (Input.GetButtonDown("Jump"))
     {
         jump = true;
         animator.SetBool("IsJumping", true);      
     } 
 }
 
 public void OnLanding()
 {
     animator.SetBool("IsJumping", false);
     jump = false;
 }
 

  void FixedUpdate()
 {
     controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
     jump = false;
     
 }

OnLanding will obviously stop the jump animation if two conditions are met:

  1. The OnLanding is called. It looks like this is meant to be used on an event. Either way, you can set a breakpoint and attach to Unity. If you never hit the breakpoint then you know that OnLanding is never called.
  2. The other condition is that your animation state machine is setup correctly. In particular, whatever animation state the machine is in must allow for the bool change to trigger a transition to another animation state.

I believe your problem is that the OnLanding() function is never called. It will only work if the animation event has been added at one of the points of the jumping animation clip. Do you know how to call the OnLanding animation event?

There is a really good animation event tutorial in this link https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html

Basically this animation event should be inserted somewhere near the end of jump animation. This will only work if you correctly connected the transition between both jump and land animation clips in the first place. Assuming that you did it, either inserting this animation event or you can try another method(more effective but a bit harder).

The physic overlap method:

This second method requires you to use a bool variable named grounded as opposed to the bool variable jump.(I know they are both boolean but their purpose are different). From there you can initialize the bool variable grounded with a built-in function named overlapcircle(). This function will require you into declare three other local variables that you will declare before hand: layermask, radius and point.

From there, you can make sure that your conditional statement contains an additional condition if grounded equal to true.

For more information on Layer-based collision detection read on this document

https://docs.unity3d.com/Manual/LayerBasedCollision.html

Feel free to ask any more questions if what I said seem unclear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM