Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Trevor D'souza · Apr 24, 2011 at 04:32 PM · countammo

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)

  1. what does Time.time represent???
  2. What does && do to the code???
  3. 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trevor D'souza · Apr 24, 2011 at 04:50 PM 0
Share

yaaa made a lot of sense thanx a lot dude =)

and happy easter :P \m/

avatar image Justin Warner · Apr 24, 2011 at 05:27 PM 0
Share

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.

avatar image
2

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. ^.^

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trevor D'souza · Apr 24, 2011 at 04:58 PM 0
Share

k got this as well thanx......

avatar image Joshua · Apr 24, 2011 at 05:02 PM 0
Share

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 ;)

avatar image by0log1c · Apr 24, 2011 at 05:20 PM 0
Share

A $$anonymous$$cher used to say: "In the domain, it ain't lazyness, its efficiency." :p

avatar image Justin Warner · Apr 24, 2011 at 05:27 PM 0
Share

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.

avatar image Joshua · Apr 24, 2011 at 05:30 PM 0
Share

Haha yeah, true xD

avatar image
1

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&gt;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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trevor D'souza · Apr 24, 2011 at 06:02 PM 0
Share

That's exactly y i was confussed :P anyways thanx

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges