I have the following code which handles jumps and double jumps:
void JumpingControls()
{
//If space is pressed and isGrounded
if (Input.GetButtonDown("Jump"))
{
if (isGrounded && !attacking)
{
animate.SetBool("Jump", true);
//set grounded to false
isGrounded = false;
//Apply jumpForce upward
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
//allowed to double jump
doubleJump = true;
}
else
{
if (doubleJump)
{
animate.SetTrigger("Double");
//not allowed to double jump
doubleJump = false;
//Apply jumpForce upward
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
}
}
}
}
void CheckIfGrounded()
{
if (isGrounded)
{
animate.SetBool("Jump", false);
doubleJump = false;
}
}
This code works great when I am detecting if I am grounded or not, but only with this collision code:
void OnCollisionEnter(Collision col)
{
if (col.contacts.Length > 0)
{
ContactPoint contact = col.contacts[0];
if (Vector3.Dot(contact.normal, Vector3.up) > 0.5f)
{
isGrounded = true;
}
else
isGrounded = false;
}
}
But there is issues with this code, if i collide with most things, it sets my isGrounded script to false, which doesnt allow me to jump, so I want to handle jump using a raycast pointing down from my feet but the issue is when I jump with the raycast code, it exits the Jump state immediately and doesn’t allow me to double jump even though it detects that I am not grounded correctly.
I need a second pair of eyes on this because I am stumped, any help appreciated, here is the raycast code:
void DetectCollision()
{
RaycastHit hit;
float dist;
Vector3 dir;
dist = 1.5f;
dir = new Vector3(0, -1, 0);
//edit: to draw ray also//
Debug.DrawRay(transform.position + new Vector3(0, 1.0f, 0), dir * dist, Color.green);
//end edit//
if (Physics.Raycast(transform.position + new Vector3(0, 1.0f, 0), dir * dist, out hit, dist))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
I have his collider set to ignore raycasts so i don't think that's the issue, but I tried it just to be sure, and still the same issue, I moved the collider up as a check like so but still the issue persists unfortunately [52187-capture.png|52187]
– JayFitz91