Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
This question was closed Nov 03, 2015 at 09:50 AM by meat5000 for the following reason:

Duplicate

avatar image
0
Question by Po-Man · Nov 01, 2015 at 01:30 PM · instantiatescript.gunreloadbullets

Need help with shooting and reload script

Hello, I have a shooting script that Instantiates bullets when the mouse0 button is pressed also it sends a ray cast to make bullet holes when the ray cast hits the walls. This is good but now I want to add a reload function ,I have an idea of how to do this but I don't really know how to put it into code (I'm not a beginner).

I would also like it to have a reload animation. thanks :)

using UnityEngine; using System.Collections;

public class Shoot : MonoBehaviour {

 public GameObject bullet;
 public GameObject bulletHole;
 public float delayTime = 2f;

 private float counter = 0f;

 void Update () 
 {

 }
 



 void FixedUpdate () 
 {



     if (Input.GetKey (KeyCode.Mouse0) && counter > delayTime) 
     {
         Instantiate(bullet, transform.position, transform.rotation);
         GetComponent<AudioSource>().Play();
         counter = 0;
     
         RaycastHit hit;
         Ray ray = new Ray(transform.position, transform.forward);
         if (Physics.Raycast(ray, out hit, 70f))
         {
             Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
         }
     }
     counter += Time.deltaTime;
 }

}

Im not sure if this is needed but here is the script for the bullet.

using UnityEngine; using System.Collections;

public class MoveBullet : MonoBehaviour { public float speed = 1f;

 void Start () 
 {
 
 }
 

 void Update () 
 {
     transform.Translate (0, 0, speed);
 }

}

Comment
Comments Locked · 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 Po-Man · Oct 31, 2015 at 04:14 PM -1
Share

Also it would be nice if I can have ammo added to it as well thanks :)

avatar image Fire_Cube · Nov 03, 2015 at 09:45 AM 0
Share

there is something you didn't specify, do you fire one bullet then reload then one bullet ect... or do you fire x bullet then reload then fire x bullet, ...?

avatar image meat5000 ♦ · Nov 03, 2015 at 09:49 AM 1
Share

Sounds suspiciously like you are requesting we do it for you.

There are seriously THOUSANDS of answers and scripts on reloading a weapon. Please search and absorb some of the knowledge. If you are not a beginner you should be doing this yourself.

Search first, try something (many things), ask for help. In that order please.

https://www.google.co.uk/search?q=unity+reload+gun&ie=utf-8&oe=utf-8&gws_rd=cr&ei=zYI4Vr$$anonymous$$8Esqfa$$anonymous$$jumLAC#q=unity+weapon+reload++script

@Fire_Cube please dont encourage script requests. We don't code to spec.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by LynoHD · Nov 03, 2015 at 10:42 AM

Im not exactly sure how to do reloading correctly, but me personally, i do it in a corutine.

and this is how i would go about doing that

 public float reloadTimer = 3f; //3 seconds
 public bool isReloading = false;
 public int bullets = 30; //bullets in weapon
 public int bulletsToReload = 30; //bullets to add when reloading
 
 public int magazines = 5; //magazines left.
 Void Update()
 {
 if(Input.getKeyDown(Keycode.R) && !isReloading) //check if pressed reload key. and check if you're not already doing a reload.
 {
 if(magazines <= 0) return; //stop if you do not have enough magazines
 StartCorutine(Reload()); //starts the Reload Corutine <- might have spelled corutine wrong.
 }
 }
 
 public IEnumerator Reload()
 {
 isReloading = true;
 yield return new WaitForSeconds(reloadTimer);
 //add the ammo
 //play the animations, etc etc
 bullets = bulletsToReload;
 magazines--;
 isReloading = false;
 
 }
Comment
Comments Locked · 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

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Gun not reloading properly & time not deducting correctly when below 10 seconds 2 Answers

Basic Unity: Can someone explain the referencing and instantiation process? 1 Answer

Instantiate a script into an instantiated prefab 3 Answers

Reloads correctly first time, but can't reload till count hits 0 1 Answer

How to get a public int equal the same number when added in all object that have the same script? 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