Jump to content
  • Advertisement
Sign in to follow this  

Scripting: Shooting a double cannon from a flying ship.

This topic is 2935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

About the blaster gun on Terminal 0.1:
XnOKw.gif 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;


Tpos = TurretPos1;


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!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!