- Home /
What does the ammo count script mean ???
Hi im a beginner with Unity 3D and i'm trying to learn the scripting language(JavaScript). There are a few things i did not understand in the Following ammo count script
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
function Fire ()
{
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && ammoCount > 0)
- what does Time.time represent???
- What does && do to the code???
- Mathematically reloadTime(0.5) + Last shot(-10) would always be a negative number right???
Also at the end of the script there a line sayin
lastShot = Time.time;
ammoCount--;
What does this mean????
I dont feel like goin ahead without understanding the scripts.... Hopeing to fine help soon thanx... cheers
Answer by Justin Warner · Apr 24, 2011 at 04:40 PM
Time.time : http://unity3d.com/support/documentation/ScriptReference/Time-time.html
The time this frame has started (Read Only). This is the time in seconds since the start of the game.
&& Means and, just like || means or. So for that if statement to run, both conditions on the sides of the && must be true.
Time.time > reloadTime
//and
ammoCount > 0
For that math equation, maybe someone else can answer, I'm horrible with math... Let along stuff I haven't read up on =).
lastShot = Time.time;
This is saying that the last shot was shot at the current time.
amouCount--;
This makes the ammoCount variable decrease.
So, if I had:
var i = 5;
//i=5
i--;
//i = 4
--i;
//i = 3
i++;
//i = 4
++i;
//i = 5
Does that make sense?
Keep this up =) You're actually trying unlike some people here haha.
yaaa made a lot of sense thanx a lot dude =)
and happy easter :P \m/
Happy easter to you as well, and no problem. I'd rather someone do this every 10 $$anonymous$$. and not "Write me a script" every 10 $$anonymous$$... But w/e.
Answer by Joshua · Apr 24, 2011 at 04:50 PM
Nice answer, Justin. A few small additions:
if (a>b) means if (a is larger then b), < means smaller.
= and <= mean is smaller/bigger or equal to.
so if (Time.time > 5) will trigger AFTER 5 seconds, if (Time.time >= 5) will trigger AT 5 seconds.
a = 5 sets a to 5.
Often you want to not change the value of a completely but add/decrease it. This would be a = a + b. Shorthand for this is a += b.
Also often you just want to add/decrease it by one. This would be a += 1. Shorthand for this is a++.
Yep - programmers are lazy. ^.^
Good :) please select Justins answer as the accepted answer so this question wont get bumped up by the bot - and Justin will get some points ;)
A $$anonymous$$cher used to say: "In the domain, it ain't lazyness, its efficiency." :p
I don't think I need points lol... =P You need 3$$anonymous$$ so you can edit peoples and fix their code =P (I think its 3$$anonymous$$?) lol.
Answer by by0log1c · Apr 24, 2011 at 05:39 PM
Maybe what I'm adding is useless but I read about the math equation and it look very similar to a syntax I use for timers. The lastShot line is related to it, it is increased at every run. Here's how I do it:
var atkDelay:float = 1; //delay between the attack var nextAtk:float = 0; //minimum Time.time where the next attack can occur
function Update(){
if(Time.time>nextAtk){
nextAtk = Time.time+atkDelay; //minimum Time.time = Now + our delay
//Attack("!!!");
//DoMoreStuff();
}
}
Everytime the if() block occurs, the nextAtk is set to atkDelay later. That is what happens in your code, with the added verification that you also owns ammo.
Though it seems pointless to declare lastShot as -10, as I believe it will have the same effect as 0.
Your answer
Follow this Question
Related Questions
the count remains the same 0 Answers
What is wrong with my text changing script? 1 Answer
weapon system script 1 Answer
Gun Script not working help please 2 Answers
Collecting 2 items at a time by mistake. 0 Answers