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 Max 4 · May 27, 2011 at 09:30 AM · raycastrandomshotgun

Making a shotgun

I want to make a shotgun using raycasts. I have a script for a pistol and was wondering if I could edit it.

 function Update () 
 {
     pistolflash = GameObject.FindWithTag("pistolflash");
     // raycast settings
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     var localOffset = transform.position;
         // check to see if user has clicked
         if(Input.GetButtonDown("Fire1"))
         {
             // if so, did we hit anything?
             if (Physics.Raycast (localOffset, direction, hit, 400)) 
             {
                 // just so we can see the raycast
                 Debug.DrawLine (localOffset, hit.point, Color.cyan);
                 // print to console to see if we have fired
                 print("we have fired!");
                 // send damage report to object
                 hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
             }
             AudioSource.PlayClipAtPoint(gunshot,transform.position);
             Instantiate (muzzleflash, pistolflash.transform.position, transform.rotation);

         }
 }
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

3 Replies

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

Answer by demize2010 · May 27, 2011 at 09:43 AM

Hi man,

First up you need to create a for loop to do multiple rays.

The simplest way to get a shotgun affect would be to add a random XY each the origin offset vector, but you may want to think about altering the ray direction by very small degrees instead. I'll provide the offset version for you as I don't have access to unity to test out the tricker rotation stuff:

 var shotgunPellets : int = 7; //How many pellets are in each shot
 
 for (var i = 0; i < shotgunPellets; i++){ //For each pellet create a random origin and fire
 
     var randomX : float = Random.Range(-1.0,1.0);
     var randomY : float = Random.Range(-1.0,1.0);
     localOffset.y += randomY;
     localOffset.X += randomX;
 
 if (Physics.Raycast (localOffset, direction, hit, 400)) {
 //Hit code
 }
 
 }
 
 //Audio and muzzle flash code
Comment
Add comment · 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
0

Answer by Kacer · May 27, 2011 at 09:39 AM

Only difference between a gun and a shotgun, scripting wise, is that a shotgun casts more rays.

So if you want some spread to the rays from your shotgun you might be able to use something like this: Clicky

I havnt tested this script, nor do i know if it works, but it looks like something you could use :)

just add a random.range in the angle, and cast like, 9 rays or so.

Comment
Add comment · 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
0

Answer by RowleyBirkin · Mar 18, 2013 at 03:22 AM

I would appreciate a version of this that would produce a cone shaped delivery of raycasts rather than tube as it were. I am struggling to find a way to randomize a ray's direction within a cone of influence from an initial ray fired from my gun. I've been trying to wrap my head around this for quite some time!

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 Chronos-L · Mar 18, 2013 at 04:16 AM 0
Share

@rowleybirkin, this question is 2 years old, and what you are looking for is different anyway. So why don't you post this as a new question?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to prevent objects from interpenetrating eachother ? 1 Answer

Shotgun script 2 Answers

Randomized Gun Spread 1 Answer

Randomly Instantiating an object inside a shrinking sphere? 1 Answer

Keep random Instantiation from spawning ontop of other objects 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