- Home /
Change Automatic to Semi-Automatic
Hi guys, i am trying to change my gun of automatic to semi automatic but is not working :/
Here the script:
ShootCooler : float = 0.9;
var shoot : boolean;
function Update(){
if(Input.GetKeyDown ("k")){
onoff = !onoff;
if(onoff == !onoff){
ShootCooler = 0.12;
}else{
ShootCooler = 0.1
}
}
}
Thanks.
Answer by Owen-Reynolds · Nov 21, 2011 at 12:17 AM
The test if(onoff == !onoff)
is always false (how can you be equal to your opposite? It's like checking if(x==x+1)
.) Also, did you mean to use shoot
for anything? But, these things happen.
The basic idea is to set "time until next shot" when you fire, and subtract each frame. When it hits 0, you can fire again.
The Unity way to have a timer is to let time roll normally (`Time.time` is the game time in seconds -- it goes up automatically.) When you fire, set a var for when you can fire next:
// NOTE: C# code, works same in unityScript
float nextShotTime=0; // global
float shootCooler = 0.9f; // time between shots. Only change if gun can fire faster
// In update:
if(Input.GetKeyDown ("k") && Time.time >= nextShotTime){
// time delay:
nextShotTime = Time.time + shootCooler;
// shoot bullet:
}
If you fire at time 16, shootCooler is set to 16.9. The if will make sure you can't fire until the game time gets to 16.9. You don't have to subtract from "time left to next shot," since Unity is adding to Time.time for you.
But how i make to when i press k again back to Automatic? :/
Ahh...'k' toggles semi/auto. $$anonymous$$aybe change onoff into a better name, such as autofire. Then just use `if(autofire==true) shootCooler=smallNum; else shotCooler=bigNum;
Not work, not back to automatic when i press k again :/
Your answer
Follow this Question
Related Questions
How i "import" variables? 2 Answers
How i turn on/off a Object with GetKeyDown? 1 Answer
Weapon Shake 2 Answers
Import Componet ( Variable ) Error Unexpected Token 3 Answers
Function reload doesnt work 1 Answer