top of page
  • Writer's pictureLuis Hostos

Scripting: Shooting a double cannon from a flying ship.

About the blaster gun on Terminal 0.1:

The idea I had in mind was a gun that fires each shot from 2 different positions, but not at the same time, it had to be one and then the other, kind of like a switch (defined by a boolean) that changes between 2 positions every time you press the fire button.

The script requires you to set 2 transform for each instantiate position, also requires you to assign the main Rigidbody of the parent object (spaceship or whatever you have) for a very important reason: to detect the speed of the flying character (using RigidShip.velocity.sqrMagnitude) and sum that amount to the speed of the bullet, so firing doesn’t look slow and inaccurate while flying.

Here’s the script, feel free to use it:

var BulletSpeed = 0; var bulletPrefab:Transform; var fireRate = 1.0; private var nextFire = 0.0; private var OnOff : boolean = false; var TurretPos1 : Transform; var TurretPos2 : Transform; private var ShipSpeed = 0; var ShipRigid : Rigidbody; function Update (){     var RigidShip = ShipRigid.GetComponent(CharacterController);     var ShipSpeed = RigidShip.velocity.sqrMagnitude;    //     print(ShipSpeed);      Shoot ();    } function Shoot(){     if(Input.GetButton (“Fire1”)&& (Time.time > nextFire))     {         OnOff = !OnOff;         var Tpos : Transform;         if(OnOff){             Tpos = TurretPos1;             }if(!OnOff){             Tpos = TurretPos2;             }         BulletSpeed += ShipSpeed;         nextFire = Time.time + fireRate / 20;         var bullet = Instantiate(bulletPrefab, Tpos.position, transform.localRotation );         bullet.rigidbody.AddRelativeForce(transform.forward * BulletSpeed);     } }

Of course, there’s probably other ways to do that, even better ways, this was my first approach so it might change. Till next time! }

0 views0 comments

Recent Posts

See All


bottom of page