How do I get the enemy's to rotate to players and shoot at them?
So I am making a game where the enemy is frozen in place and shoots the player, if they hit the player the game resets. The player is able to destroy them by killing them with their own bullets. I got the player and the enemy to shoot but the problem I am having is that the enemy does not shoot in the direction of the player. I can either get the enemy to shoot just in a straight line or I can get it to rotate towards the player but it does not rotate fast enough. Another problem I have is that if the player stands to the enemy's left, the enemy does not shoot in their direction even when the enemy is supposed to be rotating towards the player. In other words, the enemy does not shoot to the left. Can someone help me fix this? (It is a 3d game BTW) Here is the code for when the enemy only shoots in a straight line. To make the enemy stay still I froze its positions in place.
{
public Rigidbody bulletPrefab;
public float shootSpeed = 300;
public Rigidbody self;
public bool playerInRange = false;
private float lastAttackTime = 0f;
private float fireRate = 0.5f; //how many bullets are fired/second
public Transform player = null;
public Transform Target;
void OnTriggerEnter(Collider other)
{
if (other.tag == "player")
{
playerInRange = true;
player = other.transform;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "player")
{
playerInRange = false;
player = null;
}
}
void Update()
{
transform.LookAt(Vector3.zero);
if (playerInRange)
{
//Rotate the enemy towards the player
transform.LookAt(Target);
if (Time.time - lastAttackTime >= 1f / fireRate)
{
shootBullet();
lastAttackTime = Time.time;
}
}
}
void shootBullet()
{
var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);
//Shoot the Bullet in the forward direction of the player
projectile.velocity = transform.forward * shootSpeed;
}
}
Here is the code that makes the enemy rotate towards the player but the enemy doesn't rotate fast enough and does not shoot to the left. It is practically the same code as before but I just changed a few things. {
public Rigidbody bulletPrefab;
public float shootSpeed = 300;
public GameObject target;
public Rigidbody self;
public bool playerInRange = false;
private float lastAttackTime = 0f;
private float fireRate = 0.5f; //how many bullets are fired/second
public Transform player = null;
void OnTriggerEnter(Collider other)
{
if (other.tag == "player")
{
playerInRange = true;
player = other.transform;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "player")
{
playerInRange = false;
player = null;
}
}
void Update()
{
transform.LookAt(Vector3.zero);
if (playerInRange)
{
//Rotate the enemy towards the player
transform.rotation = Quaternion.LookRotation(player.position - transform.position, transform.up);
if (Time.time - lastAttackTime >= 1f / fireRate)
{
shootBullet();
lastAttackTime = Time.time;
}
}
}
void shootBullet()
{
var projectile = Instantiate(bulletPrefab, transform.position, transform.rotation);
//Shoot the Bullet in the forward direction of the player
projectile.velocity = transform.forward * shootSpeed;
projectile.transform.rotation = transform.rotation;
}
} And if it helps here is the code I have for the enemy bullet.
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613061547im_/https://answers.unity.com/themes/thub/images/avi.jpg)