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 SuperPig_9001 · Jun 26, 2021 at 06:46 PM · 2dshotgun

How to make shotgun spread in Unity 2D (without Raycasting)

I am making a game where the character just has to shoot enemies to move on, but I wanted to add variety to my game and add more guns, like a shotgun. Problem being no matter what I try, either it doesn't hit the enemy, or the bullet spawn, but don't move. I am very new to Unity, so a clear explanation, or example would be greatly appreciated

Here is the codes:

For my regular Pistol (which works):

[RequireComponent(typeof(AudioSource))] public class Weapon : MonoBehaviour { public Transform firePoint; public GameObject bulletPrefab; public AudioClip firingSound; public int ammo = 10; public bool isFiring;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetButtonDown("Fire1") && ammo > 0)
     {
         Shoot();

     }

     if (Input.GetKey(KeyCode.R))
     {
         ammo = 10;
     }
 }

 public void Shoot()
 {
     Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
     AudioSource.PlayClipAtPoint(firingSound, new Vector3(0, 0, 0));

     isFiring = true;
     ammo--;
     isFiring = false;
 }

} For my bullet:

public float speed = 10f; public int damage = 34; public Rigidbody2D rb;

 // Start is called before the first frame update
 void Start()
 {
     rb.velocity = transform.right * speed;
 }

 void OnTriggerEnter2D(Collider2D hitInfo)
 {
     EnemyHealth enemy = hitInfo.GetComponent<EnemyHealth>();

     if (enemy != null)
     {
         enemy.TakeDamage(damage);
     }

     Destroy(gameObject); 
 }

 void OnBecameInvisible()
 {
     enabled = false;
     Destroy(gameObject);
 }

For my shotgun, I though all I have to do is add 2 more firePoints, then angle them slightly, make the bullet Instantiate there, and boom, easy as that, but no, it has caused me 10 hours of searching for different methods:

public Transform firePoint, firePoint1, firePoint2; public GameObject bulletPrefab; public AudioClip firingSound; public int ammo = 10; public bool isFiring;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetButtonDown("Fire1") && ammo > 0)
     {
         Shoot();

     }

     if (Input.GetKey(KeyCode.R))
     {
         ammo = 10;
     }
 }

 public void Shoot()
 {
     Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
     Instantiate(bulletPrefab, firePoint1.position, firePoint1.rotation);
     Instantiate(bulletPrefab, firePoint2.position, firePoint2.rotation);
     AudioSource.PlayClipAtPoint(firingSound, new Vector3(0, 0, 0));

     isFiring = true;
     ammo--;
     isFiring = false;
 }

}

Oh and my code for the gun looking at my character if you need:

public float speed = 10f; public GameObject player;

 void Update()
 {
     // Rotate the object around its local X axis at 1 degree per second
     transform.Rotate(Vector3.forward * speed * Time.deltaTime);

 }

 void LateUpdate()
 {

     this.transform.position = new Vector3(player.transform.position.x, player.transform.position.y, 0);

     Vector2 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
     float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }

Thanks in advance

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 roman_sedition · Jun 29, 2021 at 02:02 AM 0
Share

Visually I don't know what this is supposed to look like. But I understand that this is a 2D game and shotguns are usually point blank weapons. I think you could just get away with using a particle system that represents the shotgun firing, and use a simple square collider that's adjacent to the character and a child of it's transform to detect any overlapping collisions, you just enable when the shotgun fires and disable some frames afterwards. It would be simpler than having multiple 2d projectile collisions.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Jun 27, 2021 at 10:44 PM

What I do is have a bullet with a rigidbody and collider and addForce(transform.forward) in the start() then if I want to do a shotgun shot I make like 5 bullets and rotate the other 4 slightly.

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 Bicsi · Jun 28, 2021 at 11:01 PM

I havent read your code (Since you posted alot), so you might have tried this.

I would simply try running a for loop and instantiate however many bullets that you want. Then for each instantiation, making the rotation random (within your wanted limits) with Random.Range, and then adding a forward velocity or impulse force to the bullets rigidbodies should do the trick. If you dont want to use rigidbodies you could also attach a simple script to the bullet object, simply making the transform move in the transform.forward direction at a decired speed over time.

I hope this help you. It should be quite straight forward. If you need more help, feel free to ask :-)

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

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

284 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

how to make a bullet spread ( shotgun ) in unity 2D 3 Answers

Unity 2D shogun,Unity 2D shogun spread? 0 Answers

Stuttering movement when approaching boundary 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