I have a script for a cannon component of a ship, It is ment to be a game object parented to the ship.
For some reason it always fires in a different direction after the ship starts to turn, not the right direction along its forward, but a random one off in a different direction, what am I doing wrong?
var reloadtime : float;
var shot : GameObject;
var numshots : int;
enum guntype{
singlefire,
rapidfire,
chaingun,
}
var gunType : guntype;
private var loaded : boolean = true;
private var fireing : boolean = false;
private var t : Transform;
function Start(){
t = transform;
}
function FireCheck(){
switch(gunType){
case guntype.singlefire:
if (loaded){Fire();}
break;
case guntype.rapidfire:
if (loaded){Fire();}
break;
case guntype.chaingun:
if (loaded && !fireing){fireing = true; loaded = false; Fire();}
break;
}
}
function Fire(){
switch(gunType){
case guntype.singlefire:
Instantiate(shot,t.position,t.rotation);
loaded = false;
yield WaitForSeconds(reloadtime);
loaded = true;
break;
case guntype.rapidfire:
for (i=1;i < numshots; i++){
Instantiate(shot,t.position,t.rotation);
loaded = false;
yield WaitForSeconds(reloadtime);
loaded = true;
}
break;
case guntype.chaingun:
while (fireing){
var f1 : float = Mathf.Round(Input.GetAxis("Fire1"));
if (f1 != 1) {fireing = false;}
Instantiate(shot,t.position,t.rotation);
yield WaitForSeconds(0.09);
}
yield WaitForSeconds(reloadtime);
loaded = true;
break;
}
}
thanks.
edit
bullet script, the bullet has no collider and the cannon is an empty game object other than the script.
var damage : float;
private var t : Transform;
function Start(){
t = transform;
print(t.forward);
Destroy(gameObject, 10);
}
function FixedUpdate(){
var ray : RaycastHit;
if (Physics.Raycast(t.position, t.forward, ray, 2))
{
ray.transform.gameObject.SendMessage("Damage",damage,SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
else{t.Translate(t.forward*2);}
}
edit : video of problem here - YouTube