Problem when spawning my AI

Hi guys!

This is my first time on this forum.
I am creating a script to drop a turret so it can kill ennemy. My turret aim ennemy and kill them, all fine.
But when I add the TurretManager script, the turret are still fine if they are put in the scene as a prefab, but not when I spawn them with my player by pressing “e”.

The turret that is spawned is aiming at the enemy, but the bullet are always going in the same direction no matter where my ennemy is (the canon follow the enmy, but bullet still going in the Z direction aka forward).

I think it has to do something about my Bullet script and the forward instructions, can somoene confirm and help me?

3459087–274266–Bullet.cs (853 Bytes)
3459087–274267–Turret.cs (1.45 KB)
3459087–274268–TurretManager.cs (663 Bytes)
3459087–274269–Enemy.cs (3.28 KB)

Okey, something weird I noticed. The first half fractions of a seconds the bullets are going in the direction of the ennemy if I had them in the scene manualy while Unity is running, but after it goes back to “forward”.

At a glance it looks like what’s happening is when your bullet is instantiated it’s being instantiated at a rotation of Quaternion.identity which is going to be Vector3.zero. Then on the bullet the Start function runs which adds force to it in the relative direction of its rotation of Vector3.zero. Then, back in the turret script immediately after the instantiation of the bullet the rotation of the bullet is set, but it’s already got force on it going along the Vector3.zero forward direction so it’s not going the right way. Instead of setting rotation after instantiating the bullet set it during the instantiation with Quaternion.Euler (bulletSpawnPoint.transform.rotation).

I haven’t tested it and could be way off the mark but hopefully that fixes the issue for you.

1 Like

Hello Quinn.

I am quite busy so I am gonna explain later today how I solved my problem.
But in short, it works now thanks to your comment. Gonna explain later how. :slight_smile: