- Home /
Shooting in direction of mouse cursor 2d
Hello guys, I'm making 2d shooter and I want the player's gun shoot in the direction of mouse cursor. So I want bullet don't changing its direction when i move mouse. Just get a position of mouse when I click button and shoot there. But the mouse cursor can't be a camera because the game is "one area" (don't know what it's called, lol, McPixel or Super Crate Box for example :D) I've really read many tutorials but I still don't get it, please help :/ Thanks guys
Answer by Michelcyc · Dec 10, 2014 at 03:17 PM
I just solve the problem. Here is my code:
//...setting shoot direction
Vector3 shootDirection;
shootDirection = Input.mousePosition;
shootDirection.z = 0.0f;
shootDirection = Camera.main.ScreenToWorldPoint(shootDirection);
shootDirection = shootDirection-transform.position;
//...instantiating the rocket
Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
bulletInstance.velocity = new Vector2(shootDirection.x * speed, shootDirection.y * speed);
Basically what I'm doing is mapping the mouse click from screenposition(2D) to worldposition(3D) based on the main camera. After that I calculate the position of the mouse minus the position of the player so we can create a vector of direction. Then I instantiate the rocket using only the X and Y from this directional vector to set the velocity.
I based my solution in this answer, you can use it for further explanations.
Answer by sparkzbarca · Dec 28, 2013 at 12:55 AM
to aim the gun at the mouse.
gun.transform.rotation = quanternion.lookat(mouseposition in world space)
add a rigidbody to the bullet prefab
on mouse button press instantiate a bullet
bullet = instantiate(bulletprefab,gun.position + .1 * gun.forward,gun.rotation);
bullet.rigidbody.addforce(gun.forward * bulletspeed);
now you've made the gun look at the mouse and when a button is pressed you spawn a bullet traveling out from the gun.
okey I was working with that and I wrote
Vector2 gunLook = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
Camera.main.ScreenToWorldPoint (gunLook);
transform.LookAt (gunLook);
but it works strangely. I add a photo! click
thanks
ps. but when mouse is on right side, in some deegres it works very good. it has to work in 360 degrees in every distance of mouse
Answer by Unicorn-slayer · Jun 26, 2016 at 11:22 AM
This worked for me:
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize ();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate (
currentBulletPrefab,
transform.position + (Vector3)( direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody2D> ().velocity = direction * bulletVelocity;
TYS$$anonymous$$.............. BUT i cant get this code to shoot at timed intervals :( by thanks alot anyway.
oh and i edited the script to this:
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Vector3 world$$anonymous$$ousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((world$$anonymous$$ousePos - transform.position));
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(
bullet1,
transform.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;
}
}
} with 1 public gameobject called bullet so that i could shoot when clicked but still no solution to timed intervals.
Answer by mdjl4 · Jul 23, 2017 at 05:52 AM
ok so i used the above persons script (Unicorn-slayer ) and edited it to use clicking:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class shooting : MonoBehaviour {
public float bulletVelocity = 5f;
public GameObject bullet;
public GameObject bullet1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(
bullet1,
transform.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;
}
}
}