Graphics Reference
In-Depth Information
// draw this raycast so we can see what it is doing
Debug.DrawRay(myTransform.position, ((myTransform.forward +
(myTransform.right * 0.5f)) * wallAvoidDistance));
Debug.DrawRay(myTransform.position, ((myTransform.forward +
(myTransform.right * -0.5f)) * wallAvoidDistance));
// cast a ray out forward from our AI and put the 'result'
// into the variable named hit
if(Physics.Raycast( myTransform.position, myTransform.
forward + ( myTransform.right * 0.5f ), out hit,
wallAvoidDistance ))
{
// obstacle
// it's a left hit, so it's a type 1 right now (though
// it could change when we check on the other side)
obstacleHitType=1;
}
if(Physics.Raycast( myTransform.position,myTransform.forward +
( myTransform.right * -0.5f ), out hit, wallAvoidDistance ))
{
// obstacle
if( obstacleHitType==0 )
{
// if we haven't hit anything yet, this is a
// type 2
obstacleHitType=2;
} else {
// if we have hits on both left and right
// raycasts, it's a type 3
obstacleHitType=3;
}
}
return obstacleHitType;
}
public void TurnTowardTarget( Transform aTarget )
{
if(aTarget==null)
return;
// Calculate the target position relative to the
// target of this transform's coordinate system.
// e.g. a positive x value means the target is to
// to the right of the car, a positive z means
// the target is in front of the car
relativeTarget = rotateTransform.InverseTransformPoint
( aTarget.position ); // note we use rotateTransform as a
// rotation object rather than
// myTransform!
// Calculate the target angle
targetAngle = Mathf.Atan2 ( relativeTarget.x,
relativeTarget.z );
// Atan returns the angle in radians, convert to degrees
targetAngle *= Mathf.Rad2Deg;
Search WWH ::




Custom Search