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 /
  • Help Room /
avatar image
0
Question by WildHunt777 · Jul 20, 2020 at 10:03 AM · 2d2d-physicstopdownshootertop down shooter

Triple Shot in 2D TopDown

Hello, Unity community. What I want is to make a triple shot weapon called Trifi-Shi in my 2d Top-Down Shooter. That means if I click (the middle bullet's direction), it will instantiate three bullets - middle one is in a correct mouse direction and other two are about 45 degrees to left or right each.

My shoot system works like this: one script (Shooting) detects if player clicks and which projectile should be spawned according to weapon equiped (in enum). Then the script instantiates a bullet.

...

Then I have the second script which is attached to the projectile (Projectile) which moves the bullet to the mouse position based on which weapon I use. So now I don't know how to instantiate three bullets (if I have equipped Trifi-Shi weapon) and each of them move in other direction in regards to mouse pos.

alt text

This is my shooting script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Shooting : MonoBehaviour
 {
     GameManager gameManager;
 
     public GameObject uLaser;
     public GameObject magnumBum;
     public GameObject robboRifle;
     public GameObject shugaruGun;
     public GameObject doomBreaker;
     public GameObject trifiShi;
 
 
     float PshotDelay = 0.05f;
     float SshotDelay = 0.5f;
 
     float fireRate = 0.15f;
     float nextFire = 0.0f;
 
     bool canPshot = true;
     bool canSshot = true;
 
 
     Transform playerPos;
 
 
     void Start()
     {
         gameManager = FindObjectOfType<GameManager>();
         playerPos = GetComponent<Transform>();
     }
     void Update()
     {
         if (Input.GetButton("Fire1") && Time.time > nextFire && gameManager.pWeapon == GameManager.PrimaryWeapon.RobboRifle)
         {
             nextFire = Time.time + fireRate;
 
             FindObjectOfType<AudioManager>().Play("Player Shoot");
 
             Instantiate(robboRifle, playerPos.position, Quaternion.identity);
         }
 
         if (Input.GetMouseButtonDown(0) && canPshot && gameManager.pWeapon != GameManager.PrimaryWeapon.RobboRifle)
         {
             FindObjectOfType<AudioManager>().Play("Player Shoot");
 
             if (gameManager.pWeapon == GameManager.PrimaryWeapon.ULaser)
                 Instantiate(uLaser, playerPos.position, Quaternion.identity);
             if (gameManager.pWeapon == GameManager.PrimaryWeapon.MagnumBum)
                 Instantiate(magnumBum, playerPos.position, Quaternion.identity);
 
             canPshot = false;
             StartCoroutine(PshotDelayCoroutine());
            
         }
         if (Input.GetMouseButtonDown(1) && canSshot)
         {
             FindObjectOfType<AudioManager>().Play("Player Shoot");
 
             if (gameManager.sWeapon == GameManager.SecondaryWeapon.ShugaruGun)
                 Instantiate(shugaruGun, playerPos.position, Quaternion.identity);
             if (gameManager.sWeapon == GameManager.SecondaryWeapon.DoomBreaker)
                 Instantiate(doomBreaker, playerPos.position, Quaternion.identity);
             if (gameManager.sWeapon == GameManager.SecondaryWeapon.TrifiShi)
             {
                 Instantiate(trifiShi, playerPos.position, Quaternion.identity);
                 Instantiate(trifiShi, playerPos.position, Quaternion.identity);
                 Instantiate(trifiShi, playerPos.position, Quaternion.identity);
             }
 
             canSshot = false;
             StartCoroutine(SshotDelayCoroutine());
         }
     }
 
     IEnumerator PshotDelayCoroutine()
     {
         yield return new WaitForSeconds(PshotDelay);
         canPshot = true;
     }
 
     IEnumerator SshotDelayCoroutine()
     {
         yield return new WaitForSeconds(SshotDelay);
         canSshot = true;
     }
 }
 

And this is the projectile script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class Projectile : MonoBehaviour
 {
     public enum Weapon
     {
         ULaser,
         MagnumBum,
         RobboRifle,
         ShugaruGun,
         DoomBreaker,
         TrifiShi
     }
 
 
     public Weapon weapon;
 
     public float speed;
     public int damage;
     public GameObject shootEffect;
     public GameObject doomShootEffect;
 
     public GameObject boomZone;
 
     Vector3 tagetToLeft;
     Vector3 target;
     Vector3 tagetToRight;
 
     public int rotation_on_set = 0;
 
 
     void Start()
     {
         if (weapon == Weapon.ULaser || weapon == Weapon.RobboRifle || weapon == Weapon.DoomBreaker)
         {
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             diff.Normalize();
             float rotZ = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotation_on_set);
         }
 
         if (weapon == Weapon.MagnumBum || weapon == Weapon.ShugaruGun)
         {
             target = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
             target.z = 0;
             target.Normalize();
         }
         if(weapon == Weapon.TrifiShi)
         {
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             diff.Normalize();
             float rotZ = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotation_on_set);
         }
             
     }
 
     void Update()
     {
         if (weapon == Weapon.ULaser || weapon == Weapon.RobboRifle)
         {
             transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
             if (Vector2.Distance(transform.position, target) < 0.2f)
             {
                 Instantiate(shootEffect, transform.position, Quaternion.identity);
                 Destroy(gameObject);
             }
         }
         if (weapon == Weapon.MagnumBum || weapon == Weapon.ShugaruGun)
         {
             transform.position = transform.position + target * speed * Time.deltaTime;
             StartCoroutine(Destroy());
         }
         if(weapon == Weapon.TrifiShi)
         {
             transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
             if (Vector2.Distance(transform.position, target) < 0.2f)
             {
                 Instantiate(shootEffect, transform.position, Quaternion.identity);
                 Destroy(gameObject);
             }
         }
         if(weapon == Weapon.DoomBreaker)
         {
             transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
             if (Vector2.Distance(transform.position, target) < 0.2f)
             {
                 MakeABoom();
             }
         }
 
 
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.CompareTag("Enemy"))
         {
             Instantiate(shootEffect, transform.position, Quaternion.identity);
         }
         if (other.CompareTag("EnemyProjectile"))
         {
             Instantiate(shootEffect, transform.position, Quaternion.identity);
         }
     }
 
     IEnumerator Destroy()
     {
         yield return new WaitForSeconds(2f);
         Destroy(gameObject);
     }
 
     public void MakeABoom()
     {
         Instantiate(boomZone, transform.position, Quaternion.identity);
         Instantiate(doomShootEffect, transform.position, Quaternion.identity);
         Destroy(gameObject);
     }
 
 }







Thanks for any response as well.

[2]: /storage/temp/163429-ueso.png

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

0 Replies

· Add your reply
  • Sort: 

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

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

2D topdown: mouse rotation and shooting,2D top down: mouse rotation and shooting 1 Answer

2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers

How to make top down particles splatter in Particle system unity 2d? 0 Answers

Topdown 2D Colliders Not Working, help! 0 Answers

2D Physics Material & Colliders 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