So I have a enemy in my scene and everything works, I just cant get it to aim at my player.
The Snowballs are launched out of the launchPoint but I cant figure out how to rotate it to aim directly at the player.
Here’s the code that makes him fire:
void Fire()
{
if (!alreadyAttacked)
{
Rigidbody rb = Instantiate(Snowball, launchPoint.position, launchPoint.rotation).GetComponent<Rigidbody>();
rb.AddForce(transform.forward * XForce, ForceMode.Impulse);
rb.AddForce(transform.up * YForce, ForceMode.Impulse);
alreadyAttacked = true;
Invoke(nameof(ResetAttack), throwCoolDown);
}
}
What did you try?
Does it work? I don’t think I’ve seen anywhere a rigidbody being instantiated.
Well I can re-write it as this:
void Fire()
{
if (!alreadyAttacked)
{
GameObject projectile = Instantiate(Snowball, launchPoint.position, launchPoint.rotation);
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.AddForce(transform.forward * XForce, ForceMode.Impulse);
rb.AddForce(transform.up * YForce, ForceMode.Impulse);
alreadyAttacked = true;
Invoke(nameof(ResetAttack), throwCoolDown);
}
}
The enemy fires is the general direction to the player, but doesn’t hit the player even if the launchpoint is always rotated towards the player by adding this to the update function:
launchPoint.transform.LookAt(target.position);
If the player is higher or lower than the enemy, the enemy will just shoot straight every time.
Would you know how to fix this?