enemy Ai scripting

Hi friends i am facing problem in enemy ai scripting i am trying to target my player by moving in its same position while rotating and targeting my player but my enemy is following my player and targeting my requirement is not following player only look at player and target while moving i am using look at target function can any one help me here is my code

public class LookAtPlayer : MonoBehaviour {

    public Transform player;

public GameObject enemyProjectilePrefab;

public float timeBetweenShootsMin = 0.5f;
public float timeBetweenShotsMax = 1.0f;
public float nextShoot = 0f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	
     player = GameObject.FindWithTag("player").transform;
	transform.LookAt(player);
	if(Time.time > nextShoot)
	  {
		nextShoot = Time.time + Random.Range(timeBetweenShootsMin, timeBetweenShotsMax);
		
		Instantiate(enemyProjectilePrefab, transform.position, transform.rotation);
		
	   }
}

}

and this is the moving script

public class MoveEnemy : MonoBehaviour {

public float enemySpeed;

public float amountToMove;

public Transform myTransform;

// Use this for initialization
void Start () {
myTransform = transform;
}

// Update is called once per frame
void Update () {
	
	amountToMove = enemySpeed * Time.deltaTime;
	myTransform.Translate(Vector3.forward * amountToMove);
}

}

2 Answers

2

If you don’t want to move your player, there is no need of moving script here. In moving script Translate() method always makes your object to move depends upon the argument in that method.

i want to move enemy in z direction with out following my player in x direction

What you need is something like this:

   void Update(){
       myTransform.Translate(Vector3.forward * amountToMove,Space.World);
   }

You are missing the Space.World.

your code is only for movement my requirement is enemy should target player by moving in z direction but it is following and targeting my player my requirement is only targeting player while moving but not following

my enemy is following my player while moving how can i disable following player

i had applied your code but it is following my player i do not need following

You have something wrong. You either have another piece of code moving the guy towards the player or you are missing the Space.World. I tried the code and saw it working. The object moves in one: public Transform player; void Update () { transform.LookAt(player.position); transform.Translate(Vector3.forward * Time.deltaTime *10f ,Space.World); } this works I can see it in front of me right now.