- Home /
Help with firing projectiles out of turret
I'm currently making a game involving a spinning turret. However, when the turret fires projectiles out of the barrels something occurs, the balls tend to spiral around the turret rather than shoot forward out of the barrels. You can play the current build for yourself.
I think it could be two things causing this:
Since the projectile emitters I have set up for each turret are parented to a rotating object, the positions at where the projectiles come from are not set properly
the locations I've set the projectiles to instantiate from are the projectile emitters' local positions relative to their parent object. This could be causing the projectiles to spiral around the parent.
Here's the code that makes the projectiles instantiate and move forward:
GameObject clone = Instantiate(projectile, turret.localPosition, Quaternion.identity) as GameObject;
clone.GetComponent<ProjectileManager>().Activate();
clone.rigidbody.velocity = turret.forward * shotSpeed;
The location is set as local, because the projectile tends to shoot forward in the world space, not local space.
I know I'm missing something simple; I just can't think of how to solve it. If anyone is able to help me out with this, it would be much appreciated.
Answer by robertbu · Dec 10, 2013 at 11:21 PM
If you look closely, the balls are not spiraling around the barrel. Rather they are being spawned at positions other than the barrel positions, so that when turret.forward is applied, the tracks are at an unexpected angle with respect to the axis of turret rotation. I believe the fix is to just use a world position in the Instantiate():
GameObject clone = Instantiate(projectile, turret.position, Quaternion.identity) as GameObject;
As I stated: the solution was a quick and easy one. Thank you for the help. I'll try to remember this tip in the future.