• 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
Question by Owen Burk · Jan 02, 2014 at 06:21 PM · raycastmachinegun

RayCast Collision being weird

I am making a machine gun based on a RayCast script. When I hold down the mouse button it shoots correctly but when I shoot at air and then go back to a collider it doesn't make the particles show up or shoot the enemies.

 #pragma strict
 
 var Effect : Transform;
 var TheDammage = 100;
 var shooting = false;
 var random = .5;
 var random2 = .5;
 
 function Start ()
 {
     random = .5;
     random2 = .5;
 }
 
 function Update () 
 {    
     if (Input.GetMouseButtonDown(0))
     {
         shooting = true;
         Accuracy();
         Accuracy2();
         Shoot();
     }
     if (Input.GetMouseButtonUp(0))
     {
         shooting = false;
     }
 }
 
 function Accuracy ()
 {
     if(shooting)
     {
         random = .51;
         yield WaitForSeconds(.09);
         random = .49;
         yield WaitForSeconds(.08);
         random = .53;
         yield WaitForSeconds(.09);
         random = .48;
         yield WaitForSeconds(.09);
         random = .52;
         yield WaitForSeconds(.07);
         random = .47;
         yield WaitForSeconds(.08);
         AccuracyRepeat();
     }
 }
 
 function Accuracy2 ()
 {
     if(shooting)
     {
         random2 = .51;
         yield WaitForSeconds(.09);
         random2 = .47;
         yield WaitForSeconds(.1);
         random2 = .53;
         yield WaitForSeconds(.09);
         random2 = .48;
         yield WaitForSeconds(.09);
         random2 = .49;
         yield WaitForSeconds(.08);
         random2 = .52;
         yield WaitForSeconds(.07);
         Accuracy2Repeat();
     }
 }
 
 function AccuracyRepeat ()
 {
     Accuracy();
 }
 function Accuracy2Repeat ()
 {
     Accuracy2();
 }
 
 
 function Shoot () 
 {
     if(shooting)
     {
         yield WaitForSeconds(.1);
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*random, Screen.height*random2, 0));
         animation.Play("Recoil");
         if (Physics.Raycast (ray, hit, 100))
         {
         
             //Instanciate, spawn
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage,SendMessageOptions.DontRequireReceiver);
             yield WaitForSeconds(0.1);
             ShootRepeat();
         }
     }
 }
 function ShootRepeat ()
 {
     Shoot();
 }
 
Comment

People who like this

0 Show 2
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 BigRoy · Jan 02, 2014 at 10:17 PM 0
Share

While creating a post or editing it there's a small toolbar at the top of the editing field. There's an icon with 101010 at the top, that is the "Add code sample" button. Click that and paste the code in there. This will make sure indentation is preserved. (You can also edit this question and fix up the indentation.)

avatar image Owen Burk · Jan 02, 2014 at 11:54 PM 0
Share

It didn't work the first time but it did this time, thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by BigRoy · Jan 02, 2014 at 10:15 PM

It's because the Repeat() function is only called if you're hitting something.

So it should become:

 function Shoot ()
 {
     if(shooting) 
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
         if (Physics.Raycast (ray, hit, 100))
         {
          
             //Instanciate, spawn
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage,SendMessageOptions.DontRequireReceiver);
             yield WaitForSeconds(0.1);
             
         }
         Repeat();
     }
 }


Instead of making a recursive function like this you could also do:

 function Shoot ()
 {
     while(shooting) 
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
         if (Physics.Raycast (ray, hit, 100))
         {
          
             //Instanciate, spawn
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage,SendMessageOptions.DontRequireReceiver);
             
         }
         
         yield WaitForSeconds(0.1);
     }
 }

This way you could remove the Repeat() coroutine function.

Note that I also moved the yield WaitForSeconds(0.1) outside of the if statement so that it will also wait that amount of time if we missed. Otherwise if you hold down the shooting button while missing and then move your mouse over an enemy the first moment is always a direct hit.

Comment

People who like this

0 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 Owen Burk · Jan 02, 2014 at 11:56 PM 0
Share

Thanks BigRoy.

avatar image BigRoy · Jan 03, 2014 at 09:12 AM 0
Share

Make sure to accept the answer if it resolved your problem. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

parented/constrained object transform lags when moving character 0 Answers

Most efficient way to make tracer machingun fire? (raycast or rigidbody) 1 Answer

Raycast moves away from object when object is rotated. 1 Answer

Enemy Ai Field of View 0 Answers

Clicking multiple objects with same tag, how to "forget" the one clicked previously... 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