void CanIClimbThis () {
float testDistance = 20;
RaycastHit[] bumperHits;
Vector3 newForward = transform.forward;
bumperHits = Physics.RaycastAll (
transform.position,
transform.forward,
testDistance);
Debug.DrawRay(
transform.position,
transform.forward * testDistance,
Color.blue
);
if (bumperHits.Length>0) {
Debug.DrawRay(
bumperHits[0].point,
bumperHits[0].normal,
Color.red
);
// to find the left/right angle of the
// obstacle ahead of you, use this.
// Determine the cross vector for the point.
// We're interested in
// the y axis spin, so use that.
Vector3 crossAxis= -Vector3.Cross(
transform.right,
bumperHits[0].normal);
// Determine the angle at that point.
float angleAtWall = Vector3.Angle(
transform.forward,
crossAxis);
print("angle is" + angleAtWall);
if (angleAtWall>135) {
print("I can go up this");
} else {
print("I should probably go around");
}
}
}
void WhichWayToTurn () {
float testDistance = 20;
RaycastHit[] bumperHits;
Vector3 newForward = transform.forward;
bumperHits = Physics.RaycastAll (
transform.position,
transform.forward,
testDistance);
Debug.DrawRay(
transform.position,
transform.forward * testDistance,
Color.blue
);
if (bumperHits.Length>0) {
Debug.DrawRay(
bumperHits[0].point,
bumperHits[0].normal,
Color.red
);
// to find the left/right angle of the
// obstacle ahead of you, use this.
// Determine the cross vector for the point.
// We're interested in
// the y axis spin, so use that.
Vector3 crossAxis= -Vector3.Cross(
transform.up,
bumperHits[0].normal);
// Determine the angle at that point.
float angleAtWall = Vector3.Angle(
transform.forward,
crossAxis);
print("angle is" + angleAtWall);
if (angleAtWall<90) {
print("I should probably turn right");
} else {
print("I should probably turn left");
}
}
void showCollisionNormal () {
float testDistance = 20;
RaycastHit[] bumperHits;
Vector3 newForward = transform.forward;
bumperHits = Physics.RaycastAll (
transform.position,
transform.forward,
testDistance);
Debug.DrawRay(
transform.position,
transform.forward * testDistance,
Color.blue
);
if (bumperHits.Length>0) {
Debug.DrawRay(
bumperHits[0].point,
bumperHits[0].normal,
Color.red
);
}
}
Find the normal of the collider in front of you, based on a ray cast.
Here's how (in Unity3d)