bool GenericCast(Vector3 direction, out GenericCastHit hit, float distance = Mathf.Infinity) { bool result = false; Vector3 origin = rb.ColliderPosition; bool defaultQueriesStartInColliders = Physics2D.queriesStartInColliders; Physics2D.queriesStartInColliders = false; RaycastHit2D h; if (wallDetection == WallDetection.Raycast) { h = Physics2D.Raycast(origin, direction, distance, castMask.value); } else { h = Physics2D.CircleCast(origin, (rb.Radius * 0.5f), direction, distance, castMask.value); } /* RaycastHit2D auto evaluates to true or false evidently */ result = (h.collider != null); hit = new GenericCastHit(h); Physics2D.queriesStartInColliders = defaultQueriesStartInColliders; //Debug.DrawLine(origin, origin + direction * distance, Color.cyan, 0f, false); return(result); }
private bool DetectCollider(Vector3 direction, out GenericCastHit hit, float distance = 1) { bool result; RaycastHit h; Vector3 origin = controller.ColliderPosition; //result = Physics.Raycast(origin, direction, out h, distance, castMask.value); result = Physics.SphereCast(origin, (controller.ColliderRadius * 0.5f), direction, out h, distance, castMask.value); if (drawGizmos) { Debug.DrawLine(origin, origin + direction * distance, Color.yellow); } hit = new GenericCastHit(h); //// If the normal is less than our slope limit then we've hit the ground and not a wall */ //float angle = Vector3.Angle(Vector3.up, hit.normal); //if (angle < controller.SlopeLimit) //{ // hit.normal = controller.ConvertVector(hit.normal); // result = false; //} return(result); }
bool GenericCast(Vector3 direction, out GenericCastHit hit, float distance = Mathf.Infinity) { bool result = false; Vector3 origin = rb.ColliderPosition; if (rb.is3D) { RaycastHit h; if (wallDetection == WallDetection.Raycast) { result = Physics.Raycast(origin, direction, out h, distance, castMask.value); } else { result = Physics.SphereCast(origin, (rb.Radius * 0.5f), direction, out h, distance, castMask.value); } hit = new GenericCastHit(h); /* If the character is grounded and we have a result check that we've hit a wall */ if (!rb.canFly && result) { /* If the normal is less than our slope limit then we've hit the ground and not a wall */ float angle = Vector3.Angle(Vector3.up, hit.normal); if (angle < rb.slopeLimit) { hit.normal = rb.ConvertVector(hit.normal); result = false; } } } else { bool defaultQueriesStartInColliders = Physics2D.queriesStartInColliders; Physics2D.queriesStartInColliders = false; RaycastHit2D h; if (wallDetection == WallDetection.Raycast) { h = Physics2D.Raycast(origin, direction, distance, castMask.value); } else { h = Physics2D.CircleCast(origin, (rb.Radius * 0.5f), direction, distance, castMask.value); } /* RaycastHit2D auto evaluates to true or false evidently */ result = (h.collider != null); hit = new GenericCastHit(h); Physics2D.queriesStartInColliders = defaultQueriesStartInColliders; } //Debug.DrawLine(origin, origin + direction * distance, Color.cyan, 0f, false); return(result); }
private bool DetectedObstacle(Vector3 facingDir, out GenericCastHit firstHit) { facingDir = controller.ConvertVector(facingDir).normalized; Vector3[] dirs = new Vector3[3]; dirs[0] = facingDir; float orientation = SteeringHelper.VectorToOrientation(facingDir); dirs[1] = SteeringHelper.OrientationToVector(orientation + sideWhiskerAngle * Mathf.Deg2Rad); dirs[2] = SteeringHelper.OrientationToVector(orientation - sideWhiskerAngle * Mathf.Deg2Rad); return(CastWhiskers(dirs, out firstHit)); }
private bool findObstacle(Vector3 facingDir, out GenericCastHit firstHit) { facingDir = rb.convertVector(facingDir).normalized; /* Create the direction vectors */ Vector3[] dirs = new Vector3[3]; dirs[0] = facingDir; float orientation = SteeringBasics.vectorToOrientation(facingDir, rb.is3D); dirs[1] = SteeringBasics.orientationToVector(orientation + sideWhiskerAngle * Mathf.Deg2Rad, rb.is3D); dirs[2] = SteeringBasics.orientationToVector(orientation - sideWhiskerAngle * Mathf.Deg2Rad, rb.is3D); return(castWhiskers(dirs, out firstHit)); }
private bool CastWhiskers(Vector3[] dirs, out GenericCastHit firstHit) { firstHit = new GenericCastHit(); bool foundObstacle = false; for (int i = 0; i < dirs.Length; i++) { float dist = (i == 0) ? mainWhiskerLength : sideWhiskerLength; if (DetectCollider(dirs[i], out GenericCastHit hit, dist)) { foundObstacle = true; firstHit = hit; break; } } return(foundObstacle); }
private bool castWhiskers(Vector3[] dirs, out GenericCastHit firstHit) { firstHit = new GenericCastHit(); bool foundObs = false; for (int i = 0; i < dirs.Length; i++) { float dist = (i == 0) ? mainWhiskerLen : sideWhiskerLen; GenericCastHit hit; if (genericCast(dirs[i], out hit, dist)) { foundObs = true; firstHit = hit; break; } } return(foundObs); }