1
\$\begingroup\$

I'm making a game revolving around moving a ball around a space. My game has a jump mechanic, so I need to know when I'm grounded in order to determine when I'm able to jump. Best way to check this is to setup raycast from ball's origin and make it constantly face downwards. However, I'm running into an embarrassing issue: the ball keeps rotating, so I'm not able to find a constant downward ray to use for finding the ground.

I started making code that looks similar to this, which the object's local transform:

bool grounded = false;
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;

if(Physics.Raycast(ray, out hit)){
//determine if object is on ground
grounded = true;
}

This caused the issue I stated earlier, where the ray would point downward but only relative to the object's current rotation. In other words, if the ball was upside-down, the ray would be pointing upwards.

My current code for this look similar to this, which now uses world space to determine what is 'down':

bool grounded = false;
Ray ray = new Ray(transform.position, -Vector3.up);
RaycastHit hit;

if(Physics.Raycast(ray, out hit)){
//determine if object is on ground
grounded = true;
}

However, in this instance, the ball raycast actually points directly to position Vector3(0,-1,0), which is even weirder than my initial problem and shouldn't be an issue based on other posts about this issue.

The only other way I can think of to properly determine whether the object is grounded is to see if it collides with anything. However, the other reasons I want to determine whether the player is grounded using raycast are:

  1. for determining whether the player is over an endless pit (e.g. if they fell off the world) in order to start a kill/respawn timer
  2. to ensure the player can perform a move equivalent to a butt stomp while in the air

If anyone knows how to perform a raycast on a spherical object in Unity, it's be much appreciated.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

So, about that second piece of code with the -Vector3.up reference. I was using a Debug method call to check for the ray's direction. If you're experienced in Unity, you know where this is going.

Yes, I used Debug.DrawLine() instead of Debug.DrawRay(), which caused the editor to draw a line from my player to the position (0,-1,0). I suspected this when I was about to go to sleep for the day but only tested it this morning. So, there's one issue out of the way.

I also checked to see if my ray was not unintentionally detecting my player's collider, and it seems to be properly ignoring it.

Long story short, I had already fixed my problem but had used the incorrect methods for determining it was correct. So, for those who are having issues with seeing your raycast in the editor, use Debug.DrawRay().

\$\endgroup\$
0
\$\begingroup\$

You may need to add a distance for how far the ray should extend from the Transform.position. Try this

if(Physics.Raycast(ray, out hit, <yourDistance>))
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .