- Home /
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; }
}
Answer by The_r0nin · Dec 26, 2010 at 01:49 PM
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...)
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 ?
Ok now it's working i removed the part " :gameobject " at bullit = Instantiate.. it's now working :D
Thank you ! :D
No problem. Please be sure to click the check mark next to the answer, so everyone knows that it has been solved.
Answer by thendricks · Aug 15, 2012 at 03:16 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613092559im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shoot Script Reload Help 1 Answer
Possibility of adding More than One Projectile 1 Answer
I need a turret script 4 Answers
how to make triger fireball not to pass through spesific objects 2 Answers
Easy Animation/Modeling question 2 Answers