- Home /
Raycast and colliders problem. Corners of two colliders
Hello
Unity 3D stealth game:
I have an enemy character (it is called a 'porter' in the game) that uses raycasting to detect collisions. An error occurs when the enemy reaches the corner of two colliders, for example the corner of two walls.
The enemy character will stop at the walls but can glitch and go through the corners particularly if rotating whilst also trying to move forward (as if pressing up arrow and either left or right arrow on keyboard simultaneously).
Other than corners between colliders the ray casting works as intended.
I've had many hours looking around through other answers and the docs but have not seen this particular problem mentioned. Other than some people suggesting to use rigidbody physics instead.
Currently I'm using transform.Translate to move the enemy character.
Note - the reason I'm drawing the ray using -transform.right is due to the setup of the 3d model imported from blender. As mentioned, this works fine except at corners.
Thanks in advance for any help
Here is the raycasting code:
// FORWARD raycasts
for (int i = 0; i<collisionDivisionsY; i++) {
float x = p.x + c.x + s.x / 2 * dir; //will put ray at left or right based on x direction
float y = p.y + c.y - s.y / 2 + s.y / (collisionDivisionsY - 1) * i; // bottom, centre and then top most point of collider
float z = p.z;
ray = new Ray (new Vector3 (x, y, z), -transform.right);
Debug.DrawRay (ray.origin, ray.direction);
if (Physics.Raycast (ray, out hit, Mathf.Abs (deltaX) + skin, collisionMask) || Physics.Raycast (ray, out hit, Mathf.Abs (deltaY) + skin, collisionMask) || Physics.Raycast (ray, out hit, Mathf.Abs (deltaZ) + skin, collisionMask)) {
Debug.Log ("hit = " + hit.distance);
//get distance between porter position and collider
float dst = Vector3.Distance (ray.origin, hit.point);
//stop porter's lateral movement after coming within skinwidth of collider
if (dst > skin) {
deltaX = dst * dir - skin * dir;
} else {
deltaZ = 0;
}
movementStopped = true;
break;
}
Your answer
Follow this Question
Related Questions
What is wrong about this RaycastHit Event??? 1 Answer
Cube clips and falls through wall when held 0 Answers
Surface with hole and Raycast - Which collider 1 Answer
Make Ray hit own collider 1 Answer
Held cube clips through wall 0 Answers