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 /
avatar image
0
Question by fraubolat · May 29, 2020 at 06:53 PM · raycastshootingbulletshootreload

shooting problem with raycasting

hello can someone tell me why the reload isnot working and how can i do if the player hold the mouse it should shoot and if he just clicked just shoot ones here is the script. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class shootak47 : MonoBehaviour
 {
     public Transform wheretoshoot;
     float range = 100f;
     bool ishooting = false;
     bool isreloading = false;
     int bullet = 100;
     int currentbullet;
     void Start()
     {
         currentbullet = bullet;
     }
 
     // Update is called once per frame
     void Update()
     {
             if(currentbullet > 0 )
             {
                 
                 StartCoroutine(delaybetweenshots());
                 
             }
 
 
 
 
 
         if(Input.GetButton("Fire1"))
         {
             if (!ishooting)
             {
                 shooting();
                 currentbullet--;
             }
         }
         
     }
 
     public void shooting ()
     {
         if (!isreloading)
             return;
         if(!ishooting)
         {
             RaycastHit Hit;
 
             if (Physics.Raycast(wheretoshoot.position, transform.forward, out Hit, range))
             {
                 
                 Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
             }
         }
 
     }
 
 
    IEnumerator delaybetweenshots()
    {
         isreloading= false;
         yield return new WaitForSeconds(2f);
         currentbullet = bullet;
         isreloading = true;
    }
 }
 
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
1

Answer by KoenigX3 · May 29, 2020 at 07:22 PM

 const float fireDelay = 0.25f;
 float delayTimer = 0;
 
 void Start()
 {
    currentbullet = bullet;
 }
 
 void Update()
 {
    if(delayTimer > 0)
    {
       delayTimer -= Time.deltaTime;
       if(delayTimer < 0) delayTimer = 0;
    }
 
    if(Input.GetButton("Fire1"))
    {
       if(delayTimer == 0)
       {
          Shoot();
          delayTimer = fireDelay;
       }
    }
 }
 
 void Shoot()
 {
    if(currentbullet == 0) return;
    // Your shooting code with the raycast goes here

    currentbullet--;
    if(currentbullet == 0) StartCoroutine(Reload());
 }
 
 IEnumerator Reload()
 {
    yield return new WaitForSeconds(2f);
    currentbullet = bullet;
 }
Comment
Add comment · Show 3 · 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 fraubolat · May 29, 2020 at 07:47 PM 0
Share

thank you it worked but when it the currentbullet is 0 it donot reload

avatar image KoenigX3 fraubolat · May 29, 2020 at 08:04 PM 0
Share

I've just tested it, it works for me. What did you put in the Shoot() method? You only need this part:

 RaycastHit Hit;
  
 if (Physics.Raycast(wheretoshoot.position, transform.forward, out Hit, range))
 {
    Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
 }


Also check if you have put return in this method. You need to make sure that it runs those 2 lines in the end, so do not use any return aside from the one i've put there.

avatar image fraubolat KoenigX3 · May 30, 2020 at 10:08 AM 0
Share

ok now it worked thank you

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

179 People are following this question.

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

Related Questions

Random Raycast inside crosshairs 1 Answer

Ray curve for bullet gravity effect, or any way to make it 1 Answer

Problem in Shooting Accuracy 0 Answers

How to stop enemies from shooting each other 1 Answer

Shoot with ray cast angle 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