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
1
Question by omarR · Jul 31, 2013 at 03:20 PM · raycastshootingraycastingspreadshotgun

Shotgun raycast

Hello everyone I looked around for this but I could not understand since the answers were all Js and I only work with C# I have a shotgun and everything is fine it shoots.. my problem is I want a realistic spread/ multiple ray cast shooting can anyone help ? thank you.

 void RayShoot () {
         Player.animation.Play("ShotgunShoot");
         RaycastHit hit;
         Vector3 DirectionRay = transform.TransformDirection (Vector3.forward);
         Debug.DrawRay (transform.position, DirectionRay*Range, Color.red);
         ray = Camera.main.ScreenPointToRay (Input.mousePosition+Random.onUnitSphere);
     
         
         if(Physics.Raycast(ray , out hit , Range)){
                 }
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
5
Best Answer

Answer by robertbu · Jul 31, 2013 at 03:30 PM

There are three answers in this question. My answer produces a spread that skews towards the center. The answer by @Owen Reynolds produces a random square spread. My follow-up comment on this answer produces a random circular spread.

http://answers.unity3d.com/questions/501221/raycast-direction-problem.html

Comment
Add comment · Show 4 · 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 robertbu · Jul 31, 2013 at 04:01 PM 0
Share

And I though of a new way if you are using a transform.forward of some object to aim. It also skews towards the center, but it is less complex:

 #pragma strict
 
 var variance = 1.0;  // This much variance 
 var distance = 10.0; // at this distance
 
 function Update() {
     
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
         for (var i = 0; i < 30; i++) {
             var v3Offset = transform.up * Random.Range(0.0, variance);
             v3Offset = Quaternion.AngleAxis(Random.Range(0.0, 360.0), transform.forward) * v3Offset;
             var v3Hit = transform.forward * distance + v3Offset;
         
             // Position an object to test pattern
             var tr = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
             tr.localScale = Vector3(0.1, 0.1, 0.1);
             tr.position = v3Hit;
         }
     }
 }

And here is the pattern:

alt text

spread.jpg (48.3 kB)
avatar image omarR · Jul 31, 2013 at 04:07 PM 0
Share

I read what you wrote in the link you gave me but I failed to write it in C# and merge it with what I have. I'm suffering I know >< and I keep getting errors on the lines with random.range ..

avatar image omarR · Jul 31, 2013 at 04:32 PM 1
Share

thank you very much for your effort.. appreciate it :)

 void RayShoot () {
         Player.animation.Play("ShotgunShoot");
         RaycastHit hit;
         Vector3 DirectionRay = transform.TransformDirection (Vector3.forward);
         Debug.DrawRay (transform.position, DirectionRay*Range, Color.red);
         ray = Camera.main.ScreenPointToRay (Input.mousePosition+Random.onUnitSphere);
     
         
         if(Physics.Raycast(ray , out hit , Range)){
                 if(hit.collider.tag == "world"){
                 HitParticles.transform.position= hit.point;
                     HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
                     HitParticles.Emit();
                     GameObject Shot;
                     Shot = Instantiate (Shot$$anonymous$$ark , hit.point,transform.rotation) as GameObject;
                     Shot.transform.LookAt(hit.point + hit.normal);
                 
             }
             if(hit.collider.tag == "metal"){
                     metalhit.transform.position= hit.point;
                     metalhit.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
                     metalhit.Emit();
                     GameObject Shot;
                     Shot = Instantiate (Shot$$anonymous$$ark , hit.point,transform.rotation) as GameObject;
                     Shot.transform.LookAt(hit.point + hit.normal);
                      //Shot.transform.localRotation = Quaternion.AngleAxis (Random.value * 360  , Vector3.forward);
                 
             }
             if(hit.collider.tag == "zombie"){
                     blood.transform.position= hit.point;
                     blood.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
                     blood.Emit();
                     blood2.transform.position= hit.point;
                     blood2.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
                     blood2.Emit();
                     hit.collider.Send$$anonymous$$essageUpwards("damage", Damage,Send$$anonymous$$essageOptions.DontRequireReceiver);
         
             }
             
                 if(hit.rigidbody){
                     if(HitParticles){
                     hit.rigidbody.AddForceAtPosition(DirectionRay*force,hit.point);
                     
                 
                 }
                     }
         }
         
         BulletsLeft--;
         if(BulletsLeft <0 ){
             BulletsLeft =0;
             
         }
         if(BulletsLeft==0 ){
             Reload();
         }
         
         
         
     }
avatar image sd_trent · Apr 15, 2019 at 03:24 AM 0
Share

This is amazing! Thanks 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

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

How do I make my raycast shooting script full auto 1 Answer

Raycast is constrained to the horizontal axis 1 Answer

Can someone help me to understand why this raycast is not working properly? 1 Answer

How to make bullets spread in unity2D? 0 Answers

Raycast shooting 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