I’m modifying the code to the EVAC-City tutorial as am wanting to set it up that the enemies don’t attack unless the player is in a certain radius. Right now I’m trying to use Raycast to try to accomplish this but after I modified the code the enemy just run in one direction instead of following the player. I’m also thinking that a Trigger even might actually be better but having a hard time trying to figure out how I should go by doing that.
Here is what I have done for Raycasting:
if(Physics.Raycast (transform.position, transform.forward, out sight, 3)) {
if(sight.collider.gameObject.tag=="Player"){
inputMovement = objPlayer.transform.position - transform.position;
inputRotation = inputMovement; // face the direction we are moving, towards the player
if ( Vector3.Distance(objPlayer.transform.position,transform.position) < 1.2f )
{
meleeAttackState = true;
inputMovement = Vector3.zero;
if (currentAnimation != animationMelee) { frameNumber = meleeAnimationMin + 1; }
}
}
}
If anyone knows what I can do to fix the enemy to where they follow the player that be great but if you know how I should implement the trigger event with the Evac-city code that will be even better.
Also since the person who wrote the tutorial has both the player and ai code in one script will it make it easier to do the trigger event by separating the code?
A raycast won’t help, because it is essential an infinitely thin line looking in one direction. In this case, the enemy would only move towards the player if the player was directly in front of him.
If you want to restrict the enemy view, the easiest way would be by using Vector3.angle to see the angle difference between the enemy forward and the direction towards the player. The distance check can also easily be done by checking the magnitude of the calculated direction.
e.g.
direction = objPlayer.transform.position - transform.position;
if (Vector3.Angle(direction, transform.forward) < 30 direction.sqrMagnitude < 3*3)
// move towards player