I want to make an enemy that is capable of shooting projectiles at the player. For this, I am using 2 C# scripts. One that controls the enemy’s AI and one that controls the projectile.
The projectile script simply gets the projectile data (such as the speed, damage, and a bool indicating if a player or an NPC shot it) and applies a velocity to its own Rigidbody using the aforementioned values. It also manages the collision system to detect if it hit a player, an enemy, or the terrain (even tho this is not relevant to the question).
The AI script controls a variety of options but the only one we are interested in is the projectile shooting function, which passes the projectile data values and instantiates a copy of the projectile prefab (which contains the projectile script).
The problem is that when it instantiates the projectile, the Quaternion.LookRotation (using it for the projectile to get shot in the direction we want it to) only takes the Y value of the objective’s GameObject transform. I have tried to manually insert random values in the Quaternion Vector3 and it seems to work fine, but will not shoot the projectile in the direction of the GameObject when trying to use its position. I have added a print statement to make sure the script knows where the GameObject is, and it does. I will attach a video with the play result and the 2 scripts.
Here is the part of the Projectile Script we are interested in:
private Rigidbody projectileRigidbody;
private float projectileSpeed;
private float projectileDamage;
private bool isPlayerShot;
private void Awake(){
projectileRigidbody = GetComponent<Rigidbody>();
}
private void Start(){
projectileRigidbody.velocity = transform.forward * projectileSpeed;
}
public void SendProjectileData(float newProjectileSpeed, float newProjectileDamage, bool newIsPlayerShot){
projectileSpeed = newProjectileSpeed;
projectileDamage = newProjectileDamage;
isPlayerShot = newIsPlayerShot;
}
And here is the Basic AI script:
[SerializeField] private bool canShoot;
[SerializeField] private float projectileDamage;
[SerializeField] private float projectileSpeed;
[SerializeField] private float shootCooldown;
private Transform shootPoint;
[SerializeField] private Transform shootObjective;
[SerializeField] private Transform projectilePrefab;
private ProjectileScript projectileScript;
// Awake function
private void Awake(){
if(canShoot == true){
shootPoint = gameObject.transform.Find("ShootPoint").transform;
projectileScript = projectilePrefab.GetComponent<ProjectileScript>();
}
}
// Actual Shoot function
private void ShootProjectile(){
if(canShoot == true){
projectileScript.SendProjectileData(projectileSpeed, projectileDamage, false);
print("The ShootObjective position is: " + shootObjective.position);
Instantiate(projectilePrefab, shootPoint.position, Quaternion.LookRotation(shootObjective.position, Vector3.up));
StartCoroutine(ShootWaitCooldown());
}
}
// Cooldown
IEnumerator ShootWaitCooldown(){
yield return new WaitForSeconds(shootCooldown);
ShootProjectile();
}