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.
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
Your answer
Follow this Question
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