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
0
Question by chrisall76 · Aug 28, 2012 at 05:37 AM · raycastgunshoot

Making a shotgun?

I've looked at other answers and they didn't really work for me. This one works with spawning the right amount of raycast but when I try to make it random it shots in a different place than where I am currently pointing at. D: Any help?

Shooting Code:

 function Shoot(){   
     //var direction = transform.TransformDirection(Vector3.forward);
     var direction : Vector3;
     var angle = 1.0;
     direction = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0f);
     var cam : Transform = Camera.main.transform;
     var ray = new Ray(cam.position, cam.forward);
     var hit : RaycastHit;
     var trf = transform; // a little optimization
     var hitRotation = transform.position;
     
     audio.PlayOneShot(shotSound); // play the shot sound...
     MuzzleFlashOn();
     
     for (var i = 0; i < shotgunPellets; i++){ //For each pellet create a random origin and fire
     
     var randomX : float = Random.Range(-0.01, 5.5);
     var randomY : float = Random.Range(-0.01, 5.5);
     hitRotation.y = randomY;
     hitRotation.x = randomX;
                
         if(Physics.Raycast (ray, hit, RayCastDist)){
         //if(Physics.Raycast(ray.origin, hitRotation, hit, RayCastDist)) Not Working Code{
 
         var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
      
             if(hit.collider.gameObject.tag == "Box"){
                 if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); 
                 hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
                 hit.collider.SendMessageUpwards("ApplyDamage", 5.0);
                 //Instantiate(bulletHole, hit.point, rot);
         }
             if(hit.collider.gameObject.tag == "Enemy"){
                 if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
                 hit.collider.SendMessageUpwards("LoseHealth", 5.0);
         } 
             if(hit.collider.gameObject.tag == "Target"){
                 hit.rigidbody.AddForceAtPosition(force * transform.forward , hit.point);
                 hit.rigidbody.isKinematic = false;
                 hit.rigidbody.useGravity = true;
                 if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); 
             } else {            
             if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
         }
         ChangeWep = false;
         SendMessageUpwards("Recoil");
         ChangeWep = true;    
         }
     }
     ThisGun.SendMessage("Lock");
     shootEnabled = false;
     yield WaitForSeconds(shotInterval);
     shootEnabled = true;
     ThisGun.SendMessage("Unlock");
 }  
Comment
Add comment · Show 1
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 Screenhog · Aug 31, 2012 at 10:10 PM 0
Share

What happens if you set it so only two bullets come out? Does the number of bullets make a difference? If it does, it's possible that the bullets are colliding with each other.

1 Reply

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

Answer by ThermalFusion · Sep 01, 2012 at 02:05 AM

The problem lies in your generation of the random ray direction. You should pass the direction as a normalized vector, not a euler angles rotation.

         var ray : Ray;
         var hit : RaycastHit;
         var tempVec : Vector3;
         for (var i : int = 0; i < shots; i++) {
             // Slerp camera forward direction towards a random direction, 0.05f is the spread, 0 means no spread, 1 means 180 degrees spread.
             tempVec = Vector3.Slerp(Camera.main.transform.forward, Random.onUnitSphere, 0.05f);
             ray = new Ray(Camera.main.transform.position, tempVec);
             if (Physics.Raycast(ray, hit, 100f)) {
                 // Do your hit stuff here
                 GameObject.Instantiate(explosion, hit.point, Quaternion.identity);
             }
         }
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 chrisall76 · Sep 01, 2012 at 11:04 PM 0
Share

Works great! thanks

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

9 People are following this question.

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

Related Questions

not losing ammo when shooting 1 Answer

How do I make a gun project a particle? 1 Answer

FPS PISTOL 1 Answer

Raycasting to the middle of the screen? 2 Answers

How do I make a gun? 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