x


Turret script TT longer reloading

Hello, this is the turret script of the tornado twins tutorial. But it works fine ! but how to I get more time between the shooting, so it takes longer for him to reload. now it is about 2 seconds.. how do i make that 5 seconds ? thanks

//CODE

var LookAtTarget:Transform; var damp = 6.0; var bullitPrefab:Transform; var savedTime;

function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

 transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

 var seconds : int = Time.time;
 var oddeven = (seconds % -2);

 if(oddeven)
 {

Shoot(seconds);

} } }

function Shoot(seconds) {

if(seconds!=savedTime) {

var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position ,
                                    Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 1000);

savedTime=seconds;
}

}

more ▼

asked Dec 26 '10 at 01:14 PM

Bram 1 gravatar image

Bram 1
75 7 8 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
var lookAtTarget:Transform; 
var damp: float = 6.0; 
var bullitPrefab:Transform; 
var savedTime: float;
var reloadTime: float = 5.0;

function Update () { 
    if(lookAtTarget) {

        var rotate: Quaternion = Quaternion.LookRotation(lookAtTarget.position - transform.position);

        transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

        var seconds : float = Time.time;

        if((seconds - savedTime) > reloadTime) {

           Shoot();
           savedTime = seconds;

        }
    }
}

function Shoot() {

    var bullit: GameObject = Instantiate(bullitPrefab , transform.Find("spawnPoint").transform.position, Quaternion.identity);
    bullit.rigidbody.AddForce(transform.forward * 1000);

}

All you want to do is save the time when you shoot, then keep checking if the time NOW is 5 seconds (or whatever your delay is) more than when you saved the time. Also, get in the habit of explicitly declaring variable types... it'll save headaches and game-cycles later... (Oh, and variable names normally start with a lowercase letter... that's sloppy coding on their part...)

more ▼

answered Dec 26 '10 at 01:49 PM

The_r0nin gravatar image

The_r0nin
1.4k 9 14 30

hello. it's not working. this is what's happening: first it waits.. and than it sprays so much rockets that it destroys itself. what's wrong ?

Dec 26 '10 at 06:58 PM Bram 1

Ok now it's working i removed the part " :gameobject " at bullit = Instantiate.. it's now working :D

Thank you ! :D

Dec 26 '10 at 07:00 PM Bram 1

No problem. Please be sure to click the check mark next to the answer, so everyone knows that it has been solved.

Dec 26 '10 at 11:34 PM The_r0nin
(comments are locked)
10|3000 characters needed characters left

I tried using the same script for my turret and all that comes up in the console is NullReferenceExeption (it looks at me but it doesn't shoot

more ▼

answered Aug 15 '12 at 03:16 PM

thendricks gravatar image

thendricks
84 3 10 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3331
x572
x172
x85

asked: Dec 26 '10 at 01:14 PM

Seen: 954 times

Last Updated: Aug 15 '12 at 03:16 PM