Rotate Bullet with rotating Turret PROPERLY

Hey all,

I am making a top down 2D shooter. I want to achieve the following:

alt text

A is the base of the turret that rotates around itself. B is the cannon where the blue bullets spawn and fire from.

My problem is that I can’t make the bullets rotate properly with the cannon. See picture B

I am using PoolManager to instantiate the bullet. Everything works fine except for the bullet rotation. Here is my code:

Turret.js

var projectile 	  : Transform;
var cannon		  : Transform;
private var spawnPosition : Vector3;

function Start () {
	Fire();
}

function Update() {
	transform.Rotate(0, 10 * Time.deltaTime, 0); // rotate turret
}

function Fire () {
	spawnPosition = cannon.position;
	var bullet  : Transform = PoolManager.Pools["ProjectilePool"].Spawn(projectile, spawnPosition, projectile.rotation);
	
	Invoke("Fire", 2);  // invoke function every 2 seconds
}

Bullet.js

var speed : float = 10;

function Update () {
	transform.Translate( Vector3(0,0,speed) * Time.deltaTime);
}

I know that I shouldn’t be using projectile.rotation in the Turret.js script but I can’t figure out what to use instead. When I use the cannon’s rotation, the bullet fires in the wrong axis.

Any help would be greatly appreciated!

Thank you!

bullet.transform.rotation = turret.transform.rotation;

First Thing , You dont need to rotate your bullet . Bullet goes straight (thats what i think u also want).

  1. You bullet will travel in forward direction only i.e bullet.transfrom.Translate(Vector3.FrowardspeedTime.deltaTime) …yours is also right
  2. Take another empty gameobject (i.e spawn_position) and attach it to turret nozzel and attach your bullet_spawn script to it. This gameobject will rotate when the turret rotates.

Thats it…you are good to go . :slight_smile: