I’m writing a simple(!) script, where player is chased by an enemy. This enemy should never actually touch the player, and like a school playground game, if the player turns around or is about to collide with enemy, the enemy would run away.
However, it’s not quite working, I keep getting a warning in regards to the following line:
if(hit.distance <= 5.0){
This is my entire update function:
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
var layerMask = 8;
var hit : RaycastHit;
var charCtrl : CharacterController = GetComponent(CharacterController);
var p1 : Vector3 = transform.position + charCtrl.center;
if (Physics.SphereCast (p1, charCtrl.height / 2, transform.forward, hit, layerMask, 10.0)) {
distanceToObstacle = hit.distance;
if(hit.distance <= 5.0){
transform.Rotate(0,25,0);
}
}
}
This my first experiment with SphereCast, so that could be the problem!
Thank you in advance!