- Home /
firing a shell from a tank turret
hey guys, im trying to have a tank shooting, so a have a tank which has a turret, the end of a turret has an empty object from where i the shell spawns and im using this script:
public var shellSpawn : Transform;
public var shell : Transform;
function Start () {
}
function Update () {
if(Input.GetKeyUp("mouse 0"))
{
var object = Instantiate(shell, shellSpawn.transform, shellSpawn.rotation);
object.
}
}
but the problem is that for some reason the shell dosnt allway spawn where its supposed to spawn
here is the code how i rotate my gun:
public var gunPrefab:Transform;
private var gun:Transform;
function Start () {
gun = Instantiate(gunPrefab,transform.position,transform.rotation);
gun.parent = transform;
}
function Update () {
var temp : Vector3;
temp.x = Input.GetAxis("Mouse Y") * (-1);
temp.y = Input.GetAxis("Mouse X") ;
temp.z = transform.rotation.z;
if((temp.x > 0 && gun.localRotation.x > 0.10) || (temp.x < 0 && gun.localRotation.x < (-0.10)))
{
temp.x = 0;
}
if((temp.y > 0 && gun.localRotation.y > 0.15) || (temp.y < 0 && gun.localRotation.y < (-0.15)))
{
temp.y = 0;
}
gun.transform.Rotate(temp);
}
You should check whether the shellspawn maybe is in a collider.
I don't know if this would do something but I would write shellSpawn.position ins$$anonymous$$d of shellSpawn.transform.
Does your tank turret turn. and if yes is the shellSpawn a child of the camera so that it turns with the turret?
i made shure its not in a colider
my tank turnes, but my shellspawn is a child of my turet, which is a child of my tank
I had a similar problem with a mortar weapon, and i found the problem was in my shell script. check whether you are adding some movement there. you could also temporarily reduce the speed at which the game runs to see exactly where it is spawned and what is happening. use Time.timeScale = 0.3;
i think that movement is not the issue, i tried it without movement
Where does it spawn sometimes. I mean is just rotated around or is it maybe sometimes behind the tank or what?
...and is it random? If you don't move and shoot two times are the two shells spawning on the same position/rotation?
Answer by Bazsee · Jun 12, 2012 at 09:43 AM
Try:
var object = Instantiate(shell, shellSpawn.transform.location + shellSpawn.transform.forward, shellSpawn.rotation);
of course then the shellSpawn object must be facing in the direction which you want to shoot, so the shell gets instantiated in front of it.
ah sry...i was at work and wasn't concentrating on the answer
Your answer
Follow this Question
Related Questions
Gun Turret will not Shoot. 1 Answer
rotating a tank turret while the tank is rotating 3 Answers
first Gun fires not well 2 Answers
Help With Turret 1 Answer
Rigidbody of empty bullet shell collision problem. 2 Answers