Jumping with raycasts causing issues?

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;
	}
}

2 Answers

2

I think the problem is you are offsetting the origin of the Raycast.

transform.position + new Vector3(0, 1.0f, 0)

If you move it out of the object it could hit its own collider. However if you are sure that the origin did not move out of the collider I would try to turn off all colliders around except ground collider and debug what your method returns.

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]

I manage to fix it, i must have had conflicting lines of code somewhere, but I converted everything into one function which checks if I am colliding and it works now.

Here is the code I am using:

void JumpingControls()
{
	RaycastHit hit;
	float dist;
	Vector3 dir;

	dist = 1.5f;
	dir = new Vector3(0, -1, 0);

	//If space is pressed and isGrounded
	if (Input.GetButtonDown("Jump"))
	{
		if (isGrounded && !attacking)
		{

			//set grounded to false
			isGrounded = false;

			//Apply jumpForce upward
			GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);

			//allowed to double jump
			doubleJump = true;

		}
	}

	if (Physics.Raycast(transform.position + new Vector3(0, 1.0f, 0), dir * dist, out hit, dist))
	{
		isGrounded = true;
		animate.SetBool("Jump", false);
	}
	else
	{
		isGrounded = false;
		animate.SetBool("Jump", true);

		if (Input.GetButtonDown("Jump") && doubleJump)
		{
			animate.SetTrigger("Double");
			//not allowed to double jump
			doubleJump = false;

			//Apply jumpForce upward
			GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
		}
	}   
}

Great ! Glad to hear that :)