Need Help Fixing my Double Jump with Animations in 3D
Hello,
I'm running into an issue getting my Double Jump to work with my animations. I tried setting up my code to work so the player can Double Jump when they hit the Jump button a second while in the air, not on the ground, and they have not exceeded the maxAirJump int. However, I can't seem to get it working. The player only jumps once. Any help with my logic would be much appreciated. Below is my C# script:
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Movements : MonoBehaviour
{
#region PUBLIC FIELDS
[Header("Ground / Landing Settings")]
[Space(5)]
public bool isGrounded; //Check if the player is on the ground
[Header ("Jump Settings")]
[Space(5)]
[SerializeField, Range(0f, 10f)]
public float jumpHeight = 10f; //Amount of height added when the player jumps.
public bool playerIsJumping; //Determines if the player is jumping or not
[Header("Movement Settings")]
[Space(5)]
public float currentSpeed; //Determines what is the player's current speed
public bool playerIsIdle = true; //Determines if the player is Idle
[Header ("Walk / Run Settings")]
[Space(5)]
public float walkSpeed; //Determines how fast the player can walk
public float runSpeed; //Determines how fast the player can run
#endregion
#region PRIVATE FIELDS
private Animator animator; //Animator for the player
private Rigidbody rb; //Rigidbody for the player
private RaycastHit m_hit; //Raychast Hit
private float m_xAxis;
private float m_zAxis;
private int jumpPhase; //Keep track of the whether another jump is allowed
private int maxAirJumps = 2; //Number of Air Jumps allowed
#endregion
#region MONODEVELOP ROUTINES
private void Awake()
{
#region Initializing Components
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
#endregion
}
private void Update()
{
#region Controller Input [ Horizontal | Vertical | Jump]
m_xAxis = Input.GetAxis("Horizontal");
m_zAxis = Input.GetAxis("Vertical");
playerIsJumping = Input.GetButton("Jump");
#endregion
}
// Update is called once per frame
private void FixedUpdate()
{
#region Move Player [ Walk | Run ]
rb.MovePosition(transform.position + Time.deltaTime * currentSpeed * transform.TransformDirection(m_xAxis, 0f, m_zAxis));
animator.SetFloat("Velocity X", m_xAxis);
animator.SetFloat("Velocity Z", m_zAxis);
#endregion
UpdateState();
if (playerIsJumping)
{
playerIsJumping = false;
PlayerJump();
}
}
#region Player's Jump State On/Off The Ground
private void OnCollisionStay (Collision collision)
{
isGrounded = true;
animator.SetBool("isGrounded", isGrounded);
PlayerJump();
}
private void OnCollisionExit (Collision collision)
{
isGrounded = false;
animator.SetBool("isGrounded", isGrounded);
}
private void UpdateState()
{
if(isGrounded)
{
jumpPhase = 0;
}
}
#endregion
#region HELPER REGIONS
private void PlayerJump()
{
//if the Jump button is pressed and the player is on the ground
if (isGrounded || jumpPhase < maxAirJumps )
{
if (Input.GetButton("Jump"))
{
jumpPhase += 1;
rb.AddForce(Vector3.up* jumpHeight, ForceMode.Impulse);
animator.SetBool("isJumping", true);
}
else if (!isGrounded && jumpPhase < maxAirJumps && Input.GetButton("Jump"))
{
animator.SetTrigger("isDoubleJump");
rb.AddForce(Vector3.up* jumpHeight, ForceMode.Impulse);
animator.SetBool("isJumping", false);
}
else
{
animator.SetBool("isJumping", false);
}
}
#endregion
}
#endregion
}
Below I have attached some screenshots of my Jump state from my animator controller. I created a Sub-State Machine for the Jumping states. The player will by default go into the Running Jump animation unless they are Idle (no Velocity X or Velocity Z).
The conditions to enter the Double Jump animation is my pressing the Jump a second (Double Jump Trigger) and they are not on the ground (isGrounded set to false).
Your answer
Follow this Question
Related Questions
Delay Animator Action in C#? 0 Answers
AnimationOverrideController does not set clip. 1 Answer
Jumping and animation script error. 2 Answers
Player animation doesn't want to stop 0 Answers