Sign in to follow this  
3uhox

Scripting: Shooting a double cannon from a flying ship.

Recommended Posts

About the blaster gun on Terminal 0.1:
[url="http://i.imgur.com/XnOKw.gif"][img]http://i.imgur.com/XnOKw.gif[/img][/url] 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:

[indent] 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);

}
}[/indent] 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this