how to create a simple AI enemy that shoot to my player

how to create a simple AI enemy that shoot to my player when he is near to the enemy

it doesnt help me at all

2 Answers

2

I have two scripts that might help. The first is a simple gun turret with an animation when it shoots. The gun turret looks at the player and only fires when the player is in proximity.

The second is a script that makes an enemy follow waypoints unless the character is in a certain proximity. When the player gets close it changes the colour of the enemy and makes it peruse.

    var player : Transform;
var safeDist : float = 15;
var currentDist : float;
var shooting : boolean = false;
var bang : AudioClip;
var bulletFab : Rigidbody;
var gunTip : Transform;
var power : float = 1000;
var shotDelay : float = 1;

function Update () {
	
	currentDist = Vector3.Distance(transform.position, player.position);
	
	if(currentDist < safeDist){
	transform.LookAt(player);
	if(!shooting) shootStuff();
	
	}
		
}

function shootStuff(){
	
	shooting = true;
	
	transform.parent.animation.Play("shoot"); //play animation	
	
	AudioSource.PlayClipAtPoint(bang, transform.position);
	
	var fwd = transform.TransformDirection(Vector3.forward);
	var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
	bulletShot.AddForce(fwd * power);
	
	yield WaitForSeconds(shotDelay);
	shooting = false;
	

}

function OnGUI(){

	GUILayout.BeginArea(Rect(10,10, 100, 100));
	GUILayout.BeginVertical("box");
	GUILayout.Label("Distance is: " + currentDist.ToString("f1"));
	
	GUILayout.EndVertical();
	GUILayout.EndArea();

<---- END OF FIRST SCRIPT ---->

<---- SECOND SCRIPT ---->

var waypoints : Transform [];
var currentPoint : int = 0;

var state : String = "patrol";
var speed : float = 10;
var chaseDist : float = 15;
var gravity : float = 15;

private var controller : CharacterController;
private var player : Transform;
private var alert : ParticleEmitter;

function Start(){
	alert = gameObject.Find("alert").particleEmitter;
	player = gameObject.FindWithTag("Player").transform;
	controller = GetComponent(CharacterController);	
}

function Update () {
	
	var playerDist = Vector3.Distance(player.position, transform.position); //vector 3.dist finds distances between things
	
	if (playerDist <= chaseDist){
		state = "chase";	
	}else{
		state = "patrol";
	}
	
	if(state == "patrol"){
		alert.emit = false;
		renderer.material.color = Color.green;
		
		if(currentPoint < waypoints.length) {
			Mover(waypoints[currentPoint].position);	
		}
		
	} else if (state == "chase"){
		alert.emit = true;
		renderer.material.color = Color.red;
		Mover(player.position);
	}
}

function Mover(target : Vector3){

	var diffVector: Vector3 = target - transform.position;
	
	var movement : Vector3;
	
	if(diffVector.magnitude > 1){
	 movement = (diffVector.normalized * speed);//without this it will run toward something and slow down when it gets close.	
	 
	}else{
		currentPoint = (currentPoint + 1) % waypoints.length;
		
	}
	
	movement.y -= gravity * Time.deltaTime;
	controller.Move(movement * Time.deltaTime);
	
	transform.LookAt(target);
	
}

ALl you have to do is combine these and that’s some very basic but useful AI.
Enjoy

the first script work fine but doest shoot to my player just look at him

Create a third shooting script with a trigger.

PLEASE combine it yourself, I have tried my best but I coould find a way to combine them.

I get the error: NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) EnemyScript2_1.Update () (at Assets/Scripts/EnemyScript2_1.js:19) But only on game start.

There are several video tutorials on youtube, as well as some written tutorials.

Google search yields:

http://www.youtube.com/watch?v=POUbAsr-6q4

http://www.youtube.com/watch?v=oytm0eYCQw4

Just have to search for exactly what you want, or be more specific as to what you are unable to find.