- Home /
Physics.RaycastAll troubles
Hi there. I'm looking to create a physics system for my characters so that they can be affected by gravity without falling through terrain. However, I'm having some trouble getting my raycasts to actually detect the colliders I need them to detect (the terrain). Here is my code:
if (velocity.y < 0)
{
// setting up variables to detect for terrain in a circular series of raycasts beneath the player.
float angle = 0;
float magnitude = 0;
List<Vector3> hitPoints = new List<Vector3>();
while (angle < 360)
{
// determine the starting point
Vector3 startPoint = transform.TransformDirection(new Vector3(Mathf.Cos(angle * Mathf.Deg2Rad), 0, Mathf.Sin(angle * Mathf.Deg2Rad)) * magnitude);
// cast a ray from the starting point going downward
RaycastHit[] rayCastHits = Physics.RaycastAll(startPoint, Vector3.up * velocity.y, Mathf.Abs(velocity.y));
// loop through the hits
foreach (RaycastHit hit in rayCastHits)
if (hit.collider.tag == "Terrain")
// add the position of the hit to a list to be looped through later
hitPoints.Add(hit.point);
// increment the magnitude and angle variables
if (magnitude == 0)
magnitude = 1;
else
angle += 60f;
}
// if the raycasts returned any hits
if (hitPoints.Count > 0)
{
// determine the highest y value of all the collisions detected
float moveToY = hitPoints[0].y;
foreach (Vector3 point in hitPoints)
if (point.y > moveToY)
moveToY = point.y;
// move to the point of the collision and reset the y velocity to zero
transform.position = new Vector3(transform.position.x, moveToY, transform.position.z);
velocity.y = 0;
}
}
My terrain object has a collider attached and is tagged as "Terrain", yet when I run this code, I don't get any collisions for my terrain. I think i may be implementing Physics.RaycastAll()
incorrectly. Is the direction
argument relative to origin
, or are they both in world coordinates? Or is there another problem with my code?
Answer by Captain_Pineapple · Dec 02, 2018 at 12:01 PM
Hey there,
as far as i understand your problem description and code i'd suggest the following changes:
Vector3 startPoint = transform.position + transform.TransformDirection(new Vector3(Mathf.Cos(angle * Mathf.Deg2Rad), 0, Mathf.Sin(angle * Mathf.Deg2Rad)) * magnitude);
RaycastHit[] rayCastHits = Physics.RaycastAll(startPoint, transform.up * velocity.y, Mathf.Abs(velocity.y));
If you use Transform.Direction it will transform a vector from local coordinates to global coordinates. But this will NOT take the position of your object into account. So for your startpoint you probably have to add your objects position to it to actually start the raycasts at the right points. Also the direction is probably not what you intended. Vector3.up is always in global coordinates. transform.up will be global coordinates for the current up vector of your object. When debugging raycasts it may help to add some Debug.DrawRay to check if you actually have the right direction and starting points.
May i ask why you even bother to do this and don't use colliders for collision detection?
$$anonymous$$y original code had some Debug.DrawLine()
calls for debugging, and it showed the proper start and end points for the Raycasts. I just changed transform.TransformDirection()
to transform.TransformPoint()
and it works now. The reason I'd used TransformDirection in the first place is because i thought that it took into account the object's rotation, whereas TransformPoint would only take into account it's position. It seems I had it backwards.
As to your question, I'm not sure what you mean, given that colliders are necessary for raycasts to return a hit anyways. I'm using this method because I find raycasts to be more consistent than colliders in my experience, since (to my knowledge) testing for a collision between two colliders only checks their current positions, whereas raycasting checks every position on a line to see if not only are they colliding, but will they collide within the next frame's worth of movement.
well if you just check the current position or also the positions where you will be with respect to your current speed depends on your settings on your rigidbody. If you choose continuous dynamic collision detection you will also take your movement into account. This should be the easier way.