- Home /
Making a shotgun shooting 45 degrees to the left of the mouse
I am making a 2D top-down shooter and attempting to make a shotgun-like weapon, I currently have a piece of the script that shoots towards the mouse and I had it instantiate two other bullets alongside of that that rotates 45 degrees but it still keeps moving along the same axes. This is the script I have that's supposed to use the Shotgun mechanic
public void Shotgun ()
{
GameObject bb = Instantiate(player.bullet, player.bulletSpawn.transform.position, Quaternion.identity);
bb.GetComponent<Rigidbody2D>().AddForce((player.mousePos - transform.position) * player.bulletSpeed);
GameObject bb2 = Instantiate(player.bullet, player.bulletSpawn2.transform.position, Quaternion.identity);
bb2.GetComponent<Rigidbody2D>().AddForce((new Vector3(player.mousePos.x, player.mousePos.y, player.mousePos.z + 45) - transform.position) * player.bulletSpeed);
GameObject bb3 = Instantiate(player.bullet, player.bulletSpawn3.transform.position, Quaternion.identity);
bb3.GetComponent<Rigidbody2D>().AddForce((new Vector3 (player.mousePos.x, player.mousePos.y, player.mousePos.z - 45) - transform.position) * player.bulletSpeed);
}
The parts of the "player" variable it's refering to is a reference to my player script which looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public Animator anim;
public float moveSpeed;
public GameObject bullet;
public GameObject bulletSpawn;
public GameObject bulletSpawn2;
public GameObject bulletSpawn3;
public float bulletSpeed;
public float stamina;
public bool hasEnoughStamina;
//[HideInInspector] public Vector2 lookDirection;
[HideInInspector] public Vector3 mousePos;
private float sprintSpeed;
private PlayerAbilities pa;
private void Start()
{
pa = gameObject.GetComponent<PlayerAbilities>();
sprintSpeed = moveSpeed *= 1.5f;
}
void Update ()
{
if(!hasEnoughStamina)
{
moveSpeed = 0.1f;
stamina += Time.deltaTime;
if (stamina >= 10)
hasEnoughStamina = true;
}
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * moveSpeed, Space.World);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * moveSpeed, Space.World);
}
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.up * moveSpeed, Space.World);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.down * moveSpeed, Space.World);
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
{
anim.SetBool("IsMoving", true);
}
if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D))
{
anim.SetBool("IsMoving", false);
}
if(Input.GetKeyDown(KeyCode.Mouse0))
{
pa.Rifle();
}
if(Input.GetKeyDown(KeyCode.Mouse1))
{
pa.Shotgun();
}
if(Input.GetKey(KeyCode.LeftShift))
{
if (hasEnoughStamina)
{
moveSpeed = sprintSpeed;
stamina -= Time.deltaTime;
if (stamina <= 0)
hasEnoughStamina = false;
}
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
moveSpeed = 0.1f;
}
}
}
If you need any images at all or any other information to help answering, I would be more than happy to give it to you. Thank you for reading this, any advice or help would be wonderful!
Answer by skaardesigns · Nov 28, 2018 at 06:04 PM
I did some researching and came across this Unity answers that I had to do some editing and translating with (https://answers.unity.com/questions/171437/shoot-at-an-angle-2d.html) and I came up with this piece of code:
public void Shotgun ()
{
Vector3 targetDelta = player.mousePos - transform.position;
Vector3 force = targetDelta.normalized * player.bulletSpeed;
Vector3 eulerAngles = new Vector3 (0, 0, 0);
GameObject bb = Instantiate(player.bullet, player.bulletSpawn.transform.position, Quaternion.identity);
bb.GetComponent<Rigidbody2D>().AddForce(targetDelta * player.bulletSpeed);
bb.GetComponent<Bullet>().damage = shotgunBulletDamage;
eulerAngles.z = -15;
Vector3 bb2Force = Quaternion.Euler(eulerAngles) * force;
GameObject bb2 = Instantiate(player.bullet, player.bulletSpawn2.transform.position, Quaternion.identity);
bb2.GetComponent<Rigidbody2D>().AddForce(bb2Force * (player.bulletSpeed/6));
bb2.GetComponent<Bullet>().damage = shotgunBulletDamage;
eulerAngles.z = 15;
Vector3 bb3Force = Quaternion.Euler(eulerAngles) * force;
GameObject bb3 = Instantiate(player.bullet, player.bulletSpawn3.transform.position, Quaternion.identity);
bb3.GetComponent<Rigidbody2D>().AddForce(bb3Force * (player.bulletSpeed/6));
bb3.GetComponent<Bullet>().damage = shotgunBulletDamage;
}
It basically creates a force and adjusts the angle of the force instead of adjusting the rotation it spawns at which gets the effect I want perfectly.
Your answer
Follow this Question
Related Questions
Creating objects which are facing center of a sphere 1 Answer
Shoot in proper direction with spread? 1 Answer
How to rotate object in instantiate with float value 1 Answer
How to Instantiate to Mouse Position 0 Answers
Network.Instatiate Problem 0 Answers