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
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.
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.
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;
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;
}
}
}