- Home /
Rotate Bullet with rotating Turret PROPERLY
Hey all,
I am making a top down 2D shooter. I want to achieve the following:
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!
Hi there, if you happen to be using a target for the turret you should possibly attach a script to the bullet prefab with Transform.LookAt(temp) where temp is = GameObject.FindWithTag("Enemy");. As I use C# I'm not sure it'll look the exact same.
Note: $$anonymous$$eep lookAt in Awake()/Start() to avoid the bullet always "looking at" the target
If it's a 2D game I have some other suggestions.
@GDIEGO - please post as a new question. Include your code.
Answer by Sisso · May 08, 2014 at 07:15 PM
bullet.transform.rotation = turret.transform.rotation;
Hey Sisso
Thanks for feedback!
I already tried this but it was no use. The projectile was spawning on the wrong axis. That's my problem
Not in your current code, you are sending the prefab rotation to poolmanager. Try to send the turret rotation.
I feel so stupid!
This worked. I had to change the rotation of my bullet projectile in the editor.
Thanks for your help man!
Answer by Noob_Vulcan · May 09, 2014 at 04:47 AM
First Thing , You dont need to rotate your bullet . Bullet goes straight (thats what i think u also want).
You bullet will travel in forward direction only i.e bullet.transfrom.Translate(Vector3.Froward*speed*Time.deltaTime) ..yours is also right
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 . :)