- Home /
"Quaternion.LookRotation" not working when Instantiate
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();
}
Answer by Captain_Pineapple · Feb 14 at 11:14 PM
Hey there,
welcome to the forum and thank you for posting a detailed and comprehensive question. Glad to put in some effort to help you here. (I really mean it)
If you read the documentation on Quaternion.LookRotation
(can be found here) you will see the following line:
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
where it says relativePos
.
so your instantiation line most likely just has to be changed to be:
Instantiate(projectilePrefab, shootPoint.position, Quaternion.LookRotation(shootObjective.position- shootPoint.position, Vector3.up));
Apart from that some advice for your script:
you have a coroutine that spawns coroutines each time it terminates. It is better style if you do it like this:
IEnumerator ShootRoutine(){
var cooldown = new WaitForSeconds(shootCooldown);
while(true) {
yield return cooldown ;
ShootProjectile(); // remove the cooldown call from this "ShootProjectile" call
}
}
This coroutine above only has to be started once and it then runs infinetly. This is better in that regard that you do not have garbage being created each time the yield return
call comes along. Downside to this method is that you cannot change the cooldown as long as the coroutine is running. (you'd have to go back to yield return new WaitForSeconds(shootCooldown)
to make that work)
Next thing is that you get the reference for projectileScript
in your start function. There you get the reference to the script instance on the prefab. When you change the projectile speed on this instance this will most probably also change the speed of all projectiles which are already in the scene.
Last thing: In case you plan on adding a lot of projectiles you should check out object pooling. This is a method to recycle short-lived objects like projectiles since the process of instantiation and destruction of objects is really bad for performance.
Let me know if that helped and good luck :)
It really worked! Thank you, it was really helpful, been struggling for a while. Thanks for your answer. Also, I don't plan to modify the cooldown in-game so I changed the coroutine method as well. I will look into the object pooling concept as well, thanks for your help.