Physics2D.Raycast() problem

I am having a problem regarding to Physics2D.Raycast()

	hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y) + RaycastStart, new Vector2(transform.position.x, transform.position.y) + RaycastEnd);
	Debug.DrawLine(new Vector3(transform.position.x, transform.position.y, ZERO) + new Vector3(RaycastStart.x, RaycastStart.y, ZERO), new Vector3(transform.position.x, transform.position.y, ZERO) + new Vector3(RaycastEnd.x, RaycastEnd.y, ZERO));

		if (hit.transform.tag == "Ground")
		{
			IsOnGround = true;
			Debug.Log("On ground");
		}
		else if (hit.transform.tag == null)
		{
			IsOnGround = false;
		}
		else
		{
			IsOnGround = false;
		}

This blocks of code is executed inside a update method. If I press the ‘Space’ key the game pawn will jump and keeps on registering ‘On ground’ even though it is almost few units away from the ‘Ground’.

Thanks in advance.

Thats because of Physics2D.Raycast distance, u don’t set, so its infinity and will hit the Ground.

Why not use?

void OnCollisionEnter2D(Collision2D coll) {
	if (coll.gameObject.tag == "Ground")
		IsOnGround = true;
}

void OnCollisionExit2D(Collision2D coll) {
	if (coll.gameObject.tag == "Ground")
		IsOnGround = false;
}

I tried to use OnCollisionEnter2D and OnCollisionExit2D. Unfortunately, it is not behaving properly. I’ll try it again because I implemented more some fixes regarding to this issue after I switch to Physics2D.Raycast. I’ll update this thread after a few minutes. Thanks

Why not use the way it is done in the 2D demo using a linecast?

Its easy to implement.

Even though I didn’t set a distance on

hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y) + RaycastStart, new Vector2(transform.position.x, transform.position.y) + RaycastEnd);

If I update my code into

hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y) + RaycastStart, new Vector2(transform.position.x, transform.position.y) + RaycastEnd);
Debug.DrawLine(new Vector3(transform.position.x, transform.position.y, ZERO) + new Vector3(RaycastStart.x, RaycastStart.y, ZERO), new Vector3(transform.position.x, transform.position.y, ZERO) + new Vector3(RaycastEnd.x, RaycastEnd.y, ZERO));

 

        if (hit.transform.tag == "Ground")
        {

            IsOnGround = true;

            Debug.Log("On ground");

        }
        else if (hit.transform.tag == null)
        {

            IsOnGround = false;
            Debug.Log("Is not on ground");
        }
        else
        {

            IsOnGround = false;
            Debug.Log("Is not on ground");
        }

it is logging ‘Is not on ground’ if my game pawn is away from the ground for about 2 - 3 units.

Another scenario, I updated my code by adding a distance argument about 0.2 afterthat, nullreferenceexception is showing out of nowhere. nullreferenceexception is not showing when my ‘Physics2d.Raycast()’ doesn’t have a argument for distance. Thanks.

@Softwizz, may you please post a code snippet for that implementation or how they implemented it? My internet is much slower than slow bro. Thanks.

First you need to put all of the gameobjects the player can jump on on a layer called ‘jumpable’ or whatever.

Then you need to add an empty gameobject to the player prefab and position it just below the collider and call this ‘GroundCheck’

Then in the script that controls the player you need to fit this into your code:

using UnityEngine;
using System.Collections;

public class jumptest : MonoBehaviour
{
	private bool grounded = false;			// Whether or not the player is grounded.
	private bool jump = false;				// Condition for whether the player should jump.
	private Transform groundCheck;			// A position marking where to check if the player is grounded.
	public float jumpForce = 250f;			// Amount of force added when the player jumps.

	void Awake()
	{
		groundCheck = transform.Find("GroundCheck"); // find the groundcheck object on the player
	}
	
	// Update is called once per frame
	void Update ()
	{
		// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
		grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("jumpable"));

		if(Input.GetKeyDown( KeyCode.Space )  grounded) // if space is pressed and the player can jump
			jump = true;

		if(jump) // if jump is true then jump
		{
			// Add a vertical force to the player.
			rigidbody2D.AddForce(new Vector2(0f, jumpForce));
			// Make sure the player can't jump again until the jump conditions from Update are satisfied.
			jump = false;
		}
	}
}

Thank you very much! :slight_smile: It helps me a lot. However, I need to switch my jumpable gameobjects that have been tagged to ‘Ground’ to ‘jumpable’ or ‘Ground’ layer.

Thanks.

Yes just add a new layer
Edit → project settings → tags and layers
create one called jumpable

Put all things the player can jump on on this layer.

You dont need to alter any object names or tags.

1485011--82509--$layersettings.png