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 Ssiroo · Oct 13, 2014 at 01:47 PM · javascriptinstantiateraycastshotgun

Shotgun Spread Issue

Hello, Im trying to make a shotgun script but im failing at making the bullets spread. The bullets instantiate fine but they go on the same direction, anyone has any idea how to fix this ?

Here is my code :

 function Update()
 {
 var hit : RaycastHit;
     var forward = transform.TransformDirection(Vector3.forward);
     
     forward.x += Random.Range(-spreadRate, spreadRate);
     forward.y += Random.Range(-spreadRate, spreadRate);
     forward.z += Random.Range(-spreadRate, spreadRate);
     
     if(Physics.Raycast(transform.position, forward, hit))
     {
         if(Input.GetButton("Fire1") && canShoot==true && playerSettings.shotgunBullets > 0 && shotgun.gameObject.active == true)
         {
                var bulletInstance : Transform;
                var bulletInstance2 : Transform;
             bulletInstance = Instantiate(bullet, barrelEnd.position, transform.rotation);
             bulletInstance2 = Instantiate(bullet, barrelEnd.position, transform.rotation);
             bulletInstance.LookAt(hit.point);
             bulletInstance2.LookAt(hit.point);
             playerSettings.shotgunBullets -= 1;
             w();
             //shotgun.animation.Play("Shoot");
             audio.PlayOneShot(soundEffect);
         }
 }


The bullet HAS to be a prefab!

Thank you for your time!

Edit : The bullet script :

 #pragma strict
 
 var bulletSpeed : float = 10;
 
 function Awake()
 {
     Destroy (this.gameObject, 5);
 }
 
 function Update()
 {
     transform.Translate(Vector3.forward * Time.deltaTime * bulletSpeed);
 }
 
 function OnCollisionEnter()
 {
     Destroy (this.gameObject);
 }
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
0

Answer by HarshadK · Oct 13, 2014 at 01:59 PM

Your bullets needs to have a LookAt target that has the spread value added to it in order to change their direction.

Check the code below:

 function Update()
 {
 var hit : RaycastHit;
     var forward = transform.TransformDirection(Vector3.forward);
  
     if(Physics.Raycast(transform.position, forward, hit))
     {
         if(Input.GetButton("Fire1") && canShoot==true && playerSettings.shotgunBullets > 0 && shotgun.gameObject.active == true)
         {
                var bulletInstance : Transform;
                var bulletInstance2 : Transform;
             bulletInstance = Instantiate(bullet, barrelEnd.position, transform.rotation);
             bulletInstance2 = Instantiate(bullet, barrelEnd.position, transform.rotation);
 
     forward.x += Random.Range(-spreadRate, spreadRate);
     forward.y += Random.Range(-spreadRate, spreadRate);
     forward.z += Random.Range(-spreadRate, spreadRate);
 
             bulletInstance.LookAt(hit.point + forward);
             bulletInstance2.LookAt(hit.point + forward);
             playerSettings.shotgunBullets -= 1;
             w();
             //shotgun.animation.Play("Shoot");
             audio.PlayOneShot(soundEffect);
         }
 }

Note: It's assumed that you move bullets based on their LookAt.

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 Ssiroo · Oct 13, 2014 at 02:18 PM 0
Share

They still move as one, not spreading around like a real shotgun bullet/shell.

avatar image HarshadK · Oct 13, 2014 at 02:21 PM 0
Share

How are you moving your bullets? Can you share that code snippet?

avatar image Ssiroo · Oct 13, 2014 at 02:26 PM 0
Share

Thank you for your quick response(x2) and I posted my script in the main topic/question.

avatar image HarshadK · Oct 14, 2014 at 09:01 AM 0
Share

What's the value of your spreadRate variable. If it's too small then you might not get a spread.

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

28 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

Related Questions

Animation not being played! 0 Answers

Make tranform follow raycast 1 Answer

AI Shooting through walls. 1 Answer

Functions being ignored 2 Answers

how to set the background image on slider 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