How do I shoot where my mouse is pointing in a 2d top down game?
Hey im making a 2d top down shooter and i just found out how to make my character shoot but he only shoots in one direction how do i make him shoot where my mouse is at?
Heres my code
Shooting:
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour {
public Vector3 bulletOffset = new Vector3(0, 0.5f, 0);
public GameObject bulletPrefab;
int bulletLayer;
public float fireDelay = 0.25f;
float cooldownTimer = 0;
void Start() {
bulletLayer = gameObject.layer;
}
// Update is called once per frame
void Update () {
cooldownTimer -= Time.deltaTime;
if( Input.GetButton("Fire1") && cooldownTimer <= 0 ) {
// SHOOT!
cooldownTimer = fireDelay;
Vector3 offset = transform.rotation * bulletOffset;
GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, transform.position + offset, transform.rotation);
bulletGO.layer = bulletLayer;
}
}
}
Answer by oStaiko · Feb 18, 2017 at 04:27 AM
What you'll want here is to calculate the angle between the x axis, and the line made by the two points being your character and the mouse. The vector math behind this looks like this:
GameObject player;
Camera camera; //You need to set up your main camera here
float nextFire = 0;
void Update ()
{
if (Input.GetButtomDown("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireDelay;
Vector2 characterPos = camera.WorldToScreenPoint(player.transform.position);
Vector2 mousePos = Input.mousePosition;
Vector2 xAxis = new Vector2 (0,1); //Represents direction of X-axis
Vector2 newLine = new Vector2(mousePos.x-characterPos.x, mousePos.y-characterPos.y);
float angle = Vector2.Angle(xAxis, newLine);
Quaternion rotation = Quaternion.identity;
rotation.eulerAngles = new Vector3(0, angle, 0); //Assuming it's top down, with only y rotations
Gameobject obj = (GameObject) Instantiate(bulletPrefab, transform.position, rotation);
obj.layer = bulletLayer;
}
}
The 'Camera.WorldToScreenPoint' is red and the ' Vector2 xAxis = (0,1)' is too, and i dont know how to fix it... sorry im new to c# I really dont understand the vector things you dont have to explain it if you want im going to try to look it up
Sorry! Yes it was a bit quick and those are just compiler errors, I'll update it in a sec
Alright ill have to test it in the morning. Thanks! (i hope this works)
I updated it again for you to include exactly what you need in your update function. I also edited your timer, it's faster to do it this way
Omg i feel like im being to needy but in what you put if (Input.GetButtonDown("Fire1" && Time.time > nextFire)) 'nextFire' is red in Quaternion rotation = Quaternion.Identity; 'Quaternion.Identity' is red and in Instantiate(bulletPrefab, transform.position, rotation).layer = bulletLayer; '.layer' is red. you did have one spelling error but i fixed it c: Again im sorry im bugging you so much about this
It is because nextFire was never declared, a pretty easy mistake to catch. I fixed it, but you really ought to learn how to discern errors yourself! It's an invaluable coding skill. Pay attention to what happens when you try to load the script in Unity, the error code will tell you how to fix it usually.
I did try to fix it myself but i did not put a number after the float nextFire
Your answer
![](https://koobas.hobune.stream/wayback/20220612111350im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 3D: Third person movement and mouse camera control 0 Answers
How to make movement speed decrease for 2D Top-down shooting game? 0 Answers
How would i change my mouse cursor in between scenes 0 Answers
Needing help with 3rd person mouse camera 1 Answer
ScreenToWorldPoint not working with cinemachine virtual camera 1 Answer