Enemys follow you

hi, im making a horror game but cant find a script to make the enemy follow me and if i hide the enemy will walk away, ive tried scripts but they dont work, like amnesia. please help.

Here is a working script that allows your enemy to follow the player, as well as (somewhat) avoid obstacles, and other mobs, then attacks the player when he’s close enough(collided). :slight_smile: Something I have been working on for about a week lOl.

Please note: You will obviously need to make a few adjustments such as the animations that are played. Hope this helps!

#pragma strict

//import System.Collections.IEnumerations ;

var Target : GameObject ;

var TargetHit : RaycastHit ;
var TargetHitLeft : RaycastHit ;
var TargetHitBack : RaycastHit ;

//var ZombieIntelligence : ZombieAI ;



class _VisionRaycastHit{
	var Back : RaycastHit ;
	var Front : RaycastHit ;
	var Left : RaycastHit ;
	var Right : RaycastHit;
}

class _VisionDirection {
	var Left : float = 0 ;
	var Right : float = 0 ;
	
	var LeftActive : boolean = false ;
	var RightActive : boolean = false ;
}

class _Vision {
	var Back : boolean = false ;
	var Front : boolean = false ;
	var Left : boolean = false ;
	var Right : boolean = false ;
	
	var EaseIn : float = 0;
	var EaseOut : float = 2 ; 
	
	var ResetFactor : float = 1 ;
	var ResetFactorActive : boolean = false ;
	
	var Hit : _VisionRaycastHit ;
	
	var VisionDirection : _VisionDirection ;
}

var Vision : _Vision ;

enum EntityState{
 	Attacking, Idle, Resting, Running, TargetingPlayer, Walking
}

class EntityTarget{
	var EntityObject : GameObject ;
	var Distance : float = 0 ;
}

class EntityIntelligence{
	var State : EntityState ; 
	
	var Target : EntityTarget ;
	
	var LastAttack : float = 0; // Current real time since last attack
	var AttackCooler : float = 0 ; // Current time since last attack ( cooler )
	var AttackCoolerDefault = 1 ; // Seconds before Can Attack
}

var EI : EntityIntelligence ; 

function Start () {
	//ZombieIntelligence = GetComponent ( ZombieAI ) ;
	EI.Target.EntityObject = GameObject.Find ( "Player" ) ;
	Target = GameObject.Find ( "Player" ) ; 
	EI.State = EntityState.TargetingPlayer ;
	
}

function Learn ( )
{
	EI.Target.Distance = (transform.position - EI.Target.EntityObject.transform.position).magnitude ;
}

function Update () {
	Learn ( ) ;
	if ( EI.State == EntityState.TargetingPlayer )
	{
		animation.Play ( "walking1InPlace" ) ;
		Vision.Front = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.forward ), Vision.Hit.Front, 25);
		Vision.Back = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.back ), Vision.Hit.Back, 15);
		Vision.Left = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.left ), Vision.Hit.Left, 15);
		Vision.Right = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.right ), Vision.Hit.Right, 15);
		var Direction : Vector3 ;
		
		Direction = ( transform.TransformDirection ( Vector3.forward ) ) ;
		
		if ( Vision.Front )
		{
			if ( Vision.Hit.Front.collider.tag == "Player" ){
				Vision.Front = false ;
			}
		}
		
		if ( Vision.Left || Vision.Right || Vision.Front || Vision.Back )
		{
			Vision.EaseIn = 0 ;
			Vision.EaseOut = 0 ;
			Vision.VisionDirection.Left = 0 ;
			Vision.VisionDirection.Right = 0 ;
		}
		
		if ( ( Vision.Front ) || ( Mathf.Ceil ( Vision.EaseIn ) != 0 && Vision.Front ) )
		{
			if ( Vision.Front ) {
				Vision.ResetFactorActive = false ;
				if ( Vision.Hit.Front.collider.tag == "Player" )
				{
					Vision.VisionDirection.Left = 0 ;
					Vision.VisionDirection.LeftActive = false ;
					Vision.VisionDirection.Right = 0 ;
					Vision.VisionDirection.RightActive = false ;
					Vision.EaseIn = 0 ; Vision.EaseOut = 0 ;
				}
				
				if ( Vision.Hit.Front.collider.tag != "Player" ){
					/*if ( Vision.Left )
					{
						Vision.VisionDirection.Right += 5 * Time.deltaTime ;
						Vision.VisionDirection.RightActive = true ;
					}else if ( Vision.Right ){
						Vision.VisionDirection.Left += 5 * Time.deltaTime ;
						Vision.VisionDirection.LeftActive = true ;
					}
					
					if ( Vision.VisionDirection.LeftActive ){
						transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x-Vision.VisionDirection.Left , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
						//Vision.EaseOut = 245 ;
				
					}else if ( Vision.VisionDirection.RightActive ){ // If Turning Right
						transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x+Vision.VisionDirection.Right , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
						Vision.EaseOut = 2 ;
				
					}else{ // Turn Right By Default 
						transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x+Vision.VisionDirection.Right , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
						Vision.EaseOut = 2 ;
				
					}*/
					
					if ( Vision.Left  && Vision.Right )
					{
						
					}else if ( Vision.Left ){
						Vision.VisionDirection.Right += 30 * Time.deltaTime;
						Vision.VisionDirection.Left = 0 ;
					}else if ( Vision.Right ){
						Vision.VisionDirection.Left += 30 * Time.deltaTime;
						Vision.VisionDirection.Right = 0 ;
					}else{
						Vision.VisionDirection.Left += 30 * Time.deltaTime;
						Vision.EaseOut = ( Vision.Hit.Front.distance / 2 ) ;
						
						if ( Vision.Front )
						{
							Vision.EaseIn = 1.5f ; 
							Vision.ResetFactor = 1 ;
						}else{
							Vision.EaseIn -= 1 * Time.deltaTime ;
						}
						
					}
					
					transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x + Vision.VisionDirection.Left - Vision.VisionDirection.Right, Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
					transform.position += (Direction/2) * Time.deltaTime ;
				}
			}else{
				
			}
		}else{
			Vision.ResetFactorActive = true ;
			if ( Mathf.Ceil ( Vision.ResetFactor ) > 0 && Vision.ResetFactorActive )
			{
				Vision.ResetFactor -= 0.2 * Time.deltaTime ;
			}else if ( Mathf.Ceil ( Vision.ResetFactor ) == 0 && Vision.ResetFactorActive )
			{
				if ( Mathf.Ceil ( Vision.EaseOut ) == 0 ){
					Vision.VisionDirection.Left = 0 ;
				}
				Vision.VisionDirection.LeftActive = false ;
				Vision.VisionDirection.Right = 0 ;
				Vision.VisionDirection.RightActive = false ;
				Vision.EaseIn = 0 ; //Vision.EaseOut = 0 ;
			}
			
			
			if ( Vision.Right )
			{
				Vision.VisionDirection.Left += 1 * Time.deltaTime ;
			}
			
			if ( Vision.Left )
			{
				Vision.VisionDirection.Right += 1 * Time.deltaTime ;
			}
			
			
			transform.position += (Direction/2) * Time.deltaTime ; 
			Vision.EaseIn = 0 ;
			if ( Vision.Right == false && Vision.Left == false ){
				if ( Mathf.Ceil ( Vision.EaseOut ) == 0  ){
					Vision.VisionDirection.LeftActive = false ;
					Vision.VisionDirection.RightActive = false ;
				}else{
					Vision.EaseOut -= 0.75 * Time.deltaTime ;
				}
			}
			
			if ( Mathf.Ceil ( Vision.EaseOut ) <= 0 ){
				Vision.VisionDirection.Left = 0 ;
				
				transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
			}
			
			/*if ( Vision.Right )
			{
				Vision.EaseOut = 2 ;
			}else if ( Vision.Left ){
				Vision.EaseOut = 2 ;
			}*/
		}
	}else if ( EI.State == EntityState.Attacking ){
		if ( !animation.IsPlaying ( "attack" ) && EI.AttackCooler < 0.1 ){
			animation.Play ( "attack" ) ;
			EI.AttackCooler = EI.AttackCoolerDefault ;
			EI.LastAttack = Time.time;
		}else if ( EI.AttackCooler > 0.1 ){
			EI.AttackCooler -= 1 * Time.deltaTime ;
		}
		DamagePlayer ( ) ;
	}
/*	if ( ZombieIntelligence.Zombie.Kombat.State.Attacking )
	{
		return true ;
	}*/
	
	
	
	
}

function DamagePlayer ( )
{
	
}

function OnCollisionEnter ( collision : Collision )
{
	Debug.Log ( collision.collider.tag ) ;
	if ( collision.collider.tag == "Player" )
	{
		EI.State = EntityState.Attacking ; 
	}
}

function OnCollisionExit ( collision : Collision )
{
	if ( collision.collider.tag == "Player" )
	{
		EI.State = EntityState.TargetingPlayer ; 
	}
}

Make sure that your target is named “Player”, or change it in the script.

hey, i put the script on the enemy and changed my name to player, but he just stays still, any ideas? thanks

var target : Transform; //the enemy’s target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
target = GameObject.FindWithTag(“Player”).transform; //target the player

}

function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

}