I have a working Enemy Code, with navigation, patrol, and all that stuff, but there is one problem: the enemies projectile LOCKS ON to the player, which makes it impossible for the player to dodge bullets. How can i make the enemy shoot at the previous location of the player? Any alternate solutions are welcome
(I am using Raycast for the projectiles btw)
Code:
private void AttackPlayer()
{
if (isDead) return;
//Make sure enemy doesn't move
agent.SetDestination(transform.position);
//look at player
transform.LookAt(player);
Debug.Log("Player Shot");
Vector3 direction = ShootingPoint.forward;
if (!alreadyAttacked)
{
//effects
MuzzleFlash.Play();
BulletEjection.Play();
//new weapon systems
MuzzleFlash.Play();
BulletEjection.Play();
print("shoot!");
if (Physics.Raycast(ShootingPoint.position, direction, out RaycastHit hit, float.MaxValue))
{
TrailRenderer trail = Instantiate(BulletTrail, ShootingPoint.position, Quaternion.identity);
StartCoroutine(SpawnTrail(trail, hit));
print("trail rendered");
}
alreadyAttacked = true;
Invoke("ResetAttack", timeBetweenAttacks);
}
GetComponent<MeshRenderer>().material = red;
}
//render trail stuff
private IEnumerator SpawnTrail(TrailRenderer Trail, RaycastHit Hit)
{
float time = 0;
Vector3 startPosition = Trail.transform.position;
while (time < 1)
{
Trail.transform.position = Vector3.Lerp(startPosition, Hit.point, time);
time += Time.deltaTime / Trail.time;
yield return null;
}
Trail.transform.position = Hit.point;
if (Hit.transform.gameObject.CompareTag("Ground") && !Hit.transform.gameObject.CompareTag("Player"))
{
ParticleSystem IPS = Instantiate(ImpactParticleSystem, Hit.point, Quaternion.LookRotation(Hit.normal));
}
if (Hit.transform.gameObject.CompareTag("Player"))
{
PlayerHealthBar.TakeDamage(10);
}
Destroy(Trail.gameObject, Trail.time);
}
private void ResetAttack()
{
if (isDead) return;
alreadyAttacked = false;
}
public void TakeDamage(int damage)
{
health -= damage;
if (health < 0)
{
isDead = true;
Invoke("Destroyy", 2.8f);
}
}