Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 yvyv1000 · Aug 03, 2016 at 06:39 AM · scripting problemshootingreloadclipammo

reload script does not work when ammo is 16

so I have the most strange script ever made. it works like a charm but when clip is 1 or 2 and totalbullets is 16 the script won't work it's soooo strange can someone tell me what might be the problem

here's the script

 var ReloadTime = 1;
 var Nifes = 3;
 var SpreadFactor : float = 0;
 var AimSpread : float = 0.01;
 var HipFireSpread : float = 0.01;
 var AimCamera : GameObject; 
 var BulletHole : GameObject;
 var Nife : GameObject;
 var NifeSpawn : GameObject;
 var IsReloading = false;
 var GunFireSound : AudioClip;
 var EmptyGunSound : AudioClip;
 var Gun : GameObject;
 var GunReloadSound : AudioClip;
 var AnimationPlayed = false;
 var MuzzleFlash : Renderer;
 
 function Start() {
     GunReloadAnimation = Gun.GetComponent.<Animator>();
     MuzzleFlash.enabled = false;
 }
 
 function Update() {
 
     AimCamera.GetComponent.<Crosshair>().enabled = true;
     SpreadFactor = HipFireSpread;
 
     if(Input.GetButton("Fire2")){
         AimCamera.GetComponent.<Crosshair>().enabled = false;
         SpreadFactor = AimSpread;
 
     }
 
     if(Input.GetButton("Fire1") && Time.time > NextFire && Clip > 0){
 
         NextFire = Time.time + FireRate;
         GetComponent.<AudioSource>().clip = GunFireSound;
         GetComponent.<AudioSource>().time = 0.2f;
         GetComponent.<AudioSource>().Play();
         Rayshooting();
         Clip --;
 
     }
     else if(Clip == 0 && TotalBullets != 0){
         GetComponent.<AudioSource>().time = 0.4f;
         GetComponent.<AudioSource>().clip = GunReloadSound;
         GetComponent.<AudioSource>().Play();
         Reload();
 
     }
     else if(Input.GetButtonDown("Fire1") && Clip == 0 && TotalBullets == 0){
         EmptySound();
 
     }
 
     if(Input.GetKey(KeyCode.R) && Clip < 30 && TotalBullets != 0 && IsReloading == false) {
         GetComponent.<AudioSource>().time = 0.4f;
         GetComponent.<AudioSource>().clip = GunReloadSound;
         GetComponent.<AudioSource>().Play();
         Reload();
 
     }
 
     if(Input.GetKeyDown(KeyCode.E) && Nifes > 0){
         Instantiate(Nife, NifeSpawn.transform.position, NifeSpawn.transform.rotation);
         Nifes --;
     }
 }
 
 function Rayshooting() {
     {
         MuzzleFlash.GetComponent.<Renderer>().enabled = true;
         yield WaitForSeconds(0.02);
         MuzzleFlash.GetComponent.<Renderer>().enabled = false;
     }
     var direction : Vector3 = transform.forward;
 
     direction.x += Random.Range(-SpreadFactor, SpreadFactor);
     direction.y += Random.Range(-SpreadFactor, SpreadFactor);
     direction.z += Random.Range(-SpreadFactor, SpreadFactor);
 
     Debug.DrawRay(transform.position,direction * BulletRange);
     var hit : RaycastHit;
 
         bHit = Physics.Raycast(transform.position,direction,hit,BulletRange);
         if (bHit && hit.transform.gameObject.tag == "Enemy"){
             hit.transform.SendMessage("DamageTaken", Damage, SendMessageOptions.DontRequireReceiver);
         }
         else if (bHit && hit.transform.gameObject.tag == "Buildings" || "Buildings2"){
             var CloneHole = Instantiate(BulletHole, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(CloneHole, 5);
         }
 
 } 
 
 function Reload() {
     if(Clip == 0 && TotalBullets >= 30){
         Clip = 30;
         TotalBullets -= 30;
         Debug.Log("Did reload 1");
     }
     if(Clip == 0 && TotalBullets << 30){
         Clip = TotalBullets;
         TotalBullets = TotalBullets - TotalBullets;
         Debug.Log("Did reload 2");
     }
     if(Clip != 0 && TotalBullets >= 30 - Clip){
         TotalBullets = TotalBullets - (30 - Clip);
         Clip = 30;
         Debug.Log("Did reload 3");
     }
     else if(Clip != 0 && TotalBullets << 30 - Clip){
         Clip = Clip + TotalBullets;
         TotalBullets = 0;
         Debug.Log("Did reload 4");
     }
 }
 
 function EmptySound(){
         GetComponent.<AudioSource>().time = 0.0f;
         GetComponent.<AudioSource>().volume = 1;
         GetComponent.<AudioSource>().clip = EmptyGunSound;
         GetComponent.<AudioSource>().Play();
 }
Comment
Add comment · Show 3
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 NordSpirit · Aug 03, 2016 at 08:37 AM 1
Share

Hi, it is look like that you have problem in you if statement on Reload() function. Try to use just "if" on line 112 ins$$anonymous$$d "else if"

avatar image yvyv1000 NordSpirit · Aug 05, 2016 at 04:18 AM 0
Share

Yupp I figured that out too and fixed it the Same day as I posted this question

avatar image Cunnah · Aug 03, 2016 at 08:37 AM 0
Share

Where is Clip declared? and is the value of Clip 1 or 2?

2 Replies

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

Answer by ScaniX · Aug 03, 2016 at 08:47 AM

You have some extraordinary code lines in there:

 if(Clip == 0 && TotalBullets << 30){

I guess that comparison should be like this instead (at both places):

 TotalBullets < 30

Otherwise you are doing some serious bitshifting here.

If you can just fill up the clip with bullets from your TotalBullets, then you should be able to make it easier like this:

  if (Clip < 30 && TotalBullets > 0) {
      int refill = Math.Min(30 - Clip, TotalBullets);
      TotalBullets -= refill;
      Clip += refill;
      Debug.Log("Refilled clip with " + refill + " bullets, bullets remaining: " + TotalBullets);
  }


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 yvyv1000 · Aug 05, 2016 at 04:17 AM 0
Share

Well I used << I seem to get errors if I don't sometimes. But I think 1 is enough then i'll change that The code worked tho I don't Have a clue First what was written but a quick look in the wiki gave me the information I needed It is basically the Same one as used by initram but the part where I don't use constants is why I will accept this script and close the thread

avatar image
0

Answer by initram · Aug 03, 2016 at 01:12 PM

There is no need for all those conditionals. Math is both simpler and faster. This code should cover all your cases.

 const maxBulletsInClip = 30;
 function Reload()
 {
     var missingBullets = maxBulletsInClip - Clip;
     var bulletsToMove = Math.min(missingBullets, TotalBullets);
     Clip += bulletsToMove;
     TotalBullets -= bulletsToMove;
 }

The reason why your code does not work is most likely because you use "left shift" instead of "less than" see here for documentation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Left_shift

You should write < instead of <<

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 yvyv1000 · Aug 05, 2016 at 04:13 AM 0
Share

I thank you for this script but I cannot use it as of Right now because by using constant i'm not able to do what I want in the current State of my project it however indeed is way cleaner than my code but im a Total noob learning hard lol only 15 years old

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

1 script doesn't react to input 1 Answer

Reload ammo with Clips using variable Substraction 1 Answer

reloading script problem 0 Answers

How To Disable/Enable Function 2 Answers

How would I add force/ burst fire to my gun? 2 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