Rotation in x,y, and z

Hi i have a script for a space flight game, and i’m trying to get the enemy to rotate towards me, Now so far they rotate to my position once but when i fly past them they don’t rotate again, they just keep shooting the other way.

var LookAtTarget:Transform;
var damp = 1.0;
var bulletPrefab:Transform;
var EnemyBulletSpeed = 1000;
var savedTime = 0;

function Update ()
{
	if(LookAtTarget)
	{
		var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
		
		transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
		
		var seconds : int = Time.time;
		var oddeven = (seconds % 2);
		
		if(oddeven)
		{
			Shoot(seconds);
		}
	}
	
}

function Shoot(seconds)

{
	if(seconds!=savedTime)
	{
	var bullet = Instantiate(bulletPrefab, transform.Find("EnemyBulletSpawn").transform.position, Quaternion.identity);
	bullet.gameObject.tag = "EnemyBullet";
	bullet.rigidbody.AddForce(transform.forward * EnemyBulletSpeed);
	
	savedTime = seconds;
}
}

thanks for any help.

One possible option would be to update the enemies rotation in LateUpdate() based on players transform, after player moves.

In my end code seems to work.

Doublecheck that you have right transform in the LookAtTarget. Make sure it is your ship’s transform, that is in scene (and not just in project) and it is really moving in your scene.

Thanks i got it sorted now… Stooopid me didn’t drag my player transform from the Hierarchy i dragged it from the project instead thats why it didn’t work properly.

I’m thinking of adding either linecast or raycast so that the enemy don’t shoot each other. :slight_smile: