Hi, I’m a bit stuck. I’ve been writing code for my player to shoot bullets at the mouse cursor.
I’ve read a lot of posts on here on how to do it but my problem is that when i instantiate my bullet it always fires to the bottom left area. The direction is affected by the position of the mouse cursor but only by a few degrees. I’ve searched for tutorials on how to do it but have come up short.
My game is in a 3D space but everything is on a 2D axis.
If anyone could offer some advice id really appreciate it.
public float bulletVelocity = 5f;
public GameObject bullet;
public GameObject bullet1;
public Transform whereBulletsFireFrom;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(transform.position);
Vector2 direction = (Vector2)((worldMousePos - (Input.mousePosition)));
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(bullet1,whereBulletsFireFrom.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = direction * bulletVelocity;
}
}
}
[\code]
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(transform.position); I think you want to pass the mouse position there. You are passing the position of this object which I assume is at origin and your direction would be from player to worldMousePosition.
You need to transform the mousePosition to world space and not your character’s position.
void Update () {
if (Input.GetMouseButtonDown(0))
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)(worldMousePos - transform.position);
direction.Normalize();
// Creates the bullet locally
GameObject bullet = (GameObject)Instantiate(bullet1,whereBulletsFireFrom.position + (Vector3)(direction * 0.5f),
Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = direction * bulletVelocity;
}
Thanks. I've altered my code to reflect you changes but now it fires bottom right and isn't at all affected by the mouse position. Maybe its something in my scene that's causing the issues. Thanks anyway
You have to account for your camera’s direction. Also, I’m not a fan of ScreenToWorldPoint at all as you’re allowing Unity to make an assumption about where your screen point’s 3d representation sits in the world.
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// It's more intuitive to just get the object's appearance on screen. Casting
// a screen point into the world is a little murky as you aren't always sure
// of where it will end up.
//
Vector3 mouse = Input.mousePosition;
Vector3 thisOnScreen = Camera.main.WorldToScreenPoint(this.transform.position);
// These screen points are relative to our camera's transform, so we need to incorporate that.
Transform camera = Camera.main.transform;
// Get the noramlized difference between our mouse and object's screen representation
Vector3 direction = (mouse - thisOnScreen).normalized;
Vector3 relative = direction.x * camera.right + direction.y * camera.up + direction.z * camera.forward;
// Sanity Check
Debug.DrawLine(this.transform.position, relative + this.transform.position, Color.red, 5f);
Vector3 adjustedSpawn = whereBulletsFireFrom.position + relative / 2;
GameObject bullet = (GameObject)Instantiate(bullet1, adjustedSpawn, Quaternion.identity);
// Adds velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = relative * bulletVelocity;
}
}
You are my hero. This code was perfect. Spent over a week trying to get it this to work. Thank you so much.
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(transform.position); I think you want to pass the mouse position there. You are passing the position of this object which I assume is at origin and your direction would be from player to worldMousePosition.
– fafase