- Home /
Not able to fire projectiles at mouse position
Hey,
I'm trying to add a simple little system that shoots projectiles to the mouses location on click. I've it added and it's shooting projectiles like I want it to, just not at the right direction. It doesn't matter where my mouse is, if my character is on the right side of the scene, the projectiles will shoot left and if it's on the left side, you guessed it - projectiles will fire away to the right.
Any idea what could be causing this? Here's the relevant function.
void Shoot () {
if (Input.GetMouseButtonDown(0)) {
Vector2 mousePos = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 myPos = new Vector2(transform.position.x,transform.position.y);
Vector2 direction = mousePos - myPos;
direction.Normalize();
Quaternion rotation = Quaternion.Euler( 0, 0, Mathf.Atan2 ( direction.y, direction.x ) * Mathf.Rad2Deg);
GameObject projectile = (GameObject)Instantiate( projectilePrefab, firePoint.position, rotation);
projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
}
}
Answer by cezikmertcan · Sep 02, 2016 at 11:45 PM
Try this
void Shoot ()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 myPos = (Vector2)transform.position;
Vector2 direction = mousePos - myPos;
direction.Normalize();
float angle = Mathf.Atan2 ( direction.y, direction.x ) * Mathf.Rad2Deg;
GameObject projectile = (GameObject)Instantiate( projectilePrefab);
projectile.transform.position = firePoint.position;
projectile.transform.Rotate (0, angle, 0);
projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
}
}
Thanks for the reply! The issue didn't change though.. Tried so many things, it's starting to feel like it's something in the settings and not in the script
I will try my code and do a better version to post here. Sorry did not have a chance to try this one.
Answer by josehzz112 · Sep 03, 2016 at 04:50 PM
I think I have a better solution
Declare public float angleOffsetInDegrees
, change it in the inspector to get the desired rotation in degrees.
void Shoot() {
if (Input.GetMouseButtonDown(0)) {
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Mouse position to world position
Vector2 myPos = new Vector2(transform.position.x, transform.position.y);
//Calculate direction in quaternion
Quaternion targetQuaternion = Quaternion.Euler(Quaternion.LookRotation(Vector3.forward, mousePos - myPos).eulerAngles + new Vector3(0f, 0f, angleOffsetInDegrees));
//Calculate direction in Vec3
Vector3 direction = new Vector3(Mathf.Sin(-targetQuaternion.eulerAngles.z * Mathf.Deg2Rad), Mathf.Cos(targetQuaternion.eulerAngles.z * Mathf.Deg2Rad), 0f);
direction.Normalize();
//Instantiate
GameObject projectile = (GameObject)Instantiate(projectilePrefab, firePoint.position, targetQuaternion);
projectile.GetComponent<Rigidbody2D>().velocity = direction * speed;
}
}
First I calculate the Quaternion setting the foward vector as the pivot for the rotation, and then the Vector3 using the Sin and Cos functions to get the corresponding angle.
Appreciate the post! That changed thing up a little bit, the projectile is now going directly north no matter where I keep my mouse
That took it back to the original issue, where if I'm standing on the left side of the screen the projectiles will fire right and vise versa. :/
Updated the code. Now you can add an offset angle value to the calculated one. Change the value of that variable in the inspector from 0 to 360 and test it to get the desired rotation angle.