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
0
Question by Rydrako · May 03, 2011 at 12:13 AM · percent

I need help calculating player shot accuracy!

Hello everyone! I was just making a small tank game and I wanted the game to calculate the player's shooting accuracy, so I coded this script -

var tanks = 1; private var tanksShot = 0;

function Start () {

     tanksShot = tanks;

}

function Update () {

     if(tanks==0)
     {
             completed = true;
     }

}

private var completed = false;

function OnGUI () {

     if(completed)
     {
             var hitPercent : float = tanksShot/Shoot.shots;
             var accuracy : int = hitPercent*100;
             var newPionts = tanksShot+accuracy;

             GUI.Box(Rect(Screen.width/2-150,Screen.height/2-150,300,300),"Level Completed");
             GUI.Label(Rect(Screen.width/2-150+20,Screen.height/2-150+20,200,30),"Tanks destroyed: "+tanksShot);
             GUI.Label(Rect(Screen.width/2-150+20,Screen.height/2-150+50,200,30),"Shots taken: "+Shoot.shots);
             GUI.Label(Rect(Screen.width/2-150+20,Screen.height/2-150+80,200,30),"Shot accuracy: "+accuracy+"%/100%");
             GUI.Label(Rect(Screen.width/2-150+20,Screen.height/2-150+110,200,30),"Pionts awarded: "+newPionts);
             if(GUI.Button(Rect(Screen.width/2-150+20,Screen.height/2-150+170,120,30),"Main menu"))
             {
                     Application.LoadLevel(0);
                     Shoot.shots = 0;
                     Shoot.canShoot = true;
                     HealthScript.LIVES = 3;
             }
             if(GUI.Button(Rect(Screen.width/2-150+160,Screen.height/2-150+170,120,30),"Continue"))
             {
                     Application.LoadLevel(Application.loadedLevel+1);
                     //reset everthing!
                     completed = false;
                     Shoot.shots = 0;
                     Shoot.canShoot = true;
                     HealthScript.LIVES = 3;
             }
     }

}

expecting it to work, but NO! For example if a player shoots 2 times, or more, to destroy one tank it will say 0&/100&, wich is really weird, so does anyone know why it's saying that?

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

1 Reply

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

Answer by Bunny83 · May 03, 2011 at 01:18 AM

Well, the problem is that if you divide an int by an int the result will be an int. You assign the result to a float variable but the calculation itself is an integer division that get truncated... 1/2 = 0.5 => 0

You have to use float variables to cast the ints into floats. In C# it's a bit simpler but in JS you have to use temp variables:

var tanksShot_F : float = tanksShot;
var shots_F : float = Shoot.shots;
var hitPercent : float = tanksShot_F/shots_F;
var accuracy : int = hitPercent*100.0;

I also changed the 100 (integer) into 100.0 (float) to be on the safe side. float * int will result in a float but it's easier to read.

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 Rydrako · May 03, 2011 at 01:26 AM 0
Share

Thanks for the help Bunny83.

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

Simple Maths Problem (I Suck At Maths) 2 Answers

how can i know a percent of animation ?? 1 Answer

Progression height affected by the width ? why 1 Answer

Percentage for random 1 Answer

Cannot cast from source type to destination type. 1 Answer


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