- Home /
AI aim and shoot projectile at Player
I am trying to make an AI enemy shoot projectiles at the player taking into account their speed to shoot in front of them in the direction they are going. The projectile detects the player and shoots every second, but is just shooting straight. I am not sure if I am calculating the target wrong or not aiming the projectile correctly.
This is a simplified version to drop into an object to test with a projectile. Any thoughts? Is there anything else I should be calculating for this?
public class AIShootProjectile : MonoBehaviour
{
public GameObject projectile;
public SphereCollider sensorTrigger = null;
public float shootForce;
public float attackCoolDown = 1;
private float timer;
private Vector3 targetpos;
private float distanceToTarget;
private Vector3 targetDirection;
private float targetSpeed;
void Update()
{
timer += Time.deltaTime;
if (timer >= attackCoolDown) // if using ammo, add (&& ammo >= 1)
{
ShootProjectile();
}
}
// AI has a large Sphere Collider attached to detect collisions of potential threats
// When there is a collider,
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Get target info
Vector3 velocity = other.attachedRigidbody.velocity;
targetSpeed = velocity.magnitude;
targetpos = other.transform.position;
distanceToTarget = Vector3.Distance(sensorTrigger.transform.position, other.transform.position);
targetDirection = other.transform.forward;
}
}
void ShootProjectile()
{
float projectileSpeed = shootForce;
float projectileTimeToTarget = distanceToTarget / projectileSpeed;
float projectedTargetTravelDistance = targetSpeed * projectileTimeToTarget;
Vector3 projectedTarget = targetDirection * projectedTargetTravelDistance;
projectedTarget.y += 1; //aim at center of target if 2m high
GameObject go = Instantiate(projectile, transform.position, Quaternion.identity);
go.transform.LookAt(projectedTarget);
Rigidbody rb = go.GetComponent<Rigidbody>();
rb.velocity = go.transform.forward * shootForce;
rb.useGravity = true;
timer = 0f;
}
}
Answer by ExtriPL · Sep 21, 2020 at 10:11 PM
Probably, you should add target position to projectedTarget
void ShootProjectile() { ... Vector3 projectedTarget = targetPosition + targetDirection * projectedTargetTravelDistance; ... }
You can also simplify code by using velocity vector instead of forward direction
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Get target info
targetVelocity = other.attachedRigidbody.velocity;
targetpos = other.transform.position;
distanceToTarget = Vector3.Distance(sensorTrigger.transform.position, other.transform.position);
}
}
void ShootProjectile()
{
float projectileTimeToTarget = distanceToTarget / shootForce;
Vector3 projectedTarget = targetPosition + targetVelocity * projectileTimeToTarget;
projectedTarget.y += 1; //aim at center of target if 2m high
...
}
Hey Thanks! Adding the target position to the projected target seemed to help and it's now shooting at the Player, but not taking into account the movement speed to shoot in front of the Player. At least one of the issues was that it's not getting the velocity of the Player so it's always Vector3 zero. I have tried a few different ways though doesnt seem to recognize the player's rigidbody.
Your answer
Follow this Question
Related Questions
Make AI enemies react to bullet hits (Solved) 0 Answers
Enemy (AI) Doesn't shoot weapon projectile, until after the player is gone. 0 Answers
How to make enemy to shoot and dodge projectile? 1 Answer
How do I apply bullet projectile to enemy ai properly on unity3d 0 Answers
how to make an enemy Ai blow up when you shoot it? 2 Answers