# Shooting towards mousePosition

Problem:

I’m having trouble getting my player to successfully shoot a projectile towards my mouse position. I need this function to work regardless of where my player is in the game.

Note:

I am using C# to code, and this game is entirely in 2D.
I am still new to Unity and coding C#. (Only about a month or so into it)

What I know:

When the player is resting at 0, 0 he can successfully shoot in any direction and the projectile will move towards and through the mouse position. However when my player falls, due to gravity, from 0, 0 to about 0, -3 aiming is completely off.

Workaround:

If I want to shoot horizontally and to the right I need to aim at 5, 0 instead of 5, -3. But this solution isn’t ideal for game play.

How my script(s) work(s):

Currently my playerControl script calls for a projectile prefab to be instantiated in front of the player when the left mouse button is clicked. Once the projectile is spawned, it uses a projectilePhysics script (which is a component of the projectile) to move in the direction of the mouse.

What I’ve done to solve this:

I’ve been scouring the web for solutions to this problem and haven’t found any that I can either understand or implement in my script successfully.

Here is my code below, thoughts?

PlayerControl:

``````void FixedUpdate () {
if(Input.GetButton("Fire1")&& !Input.GetButton("Fire2"))//FIRE MAIN & RATE OF FIRE
{
if(waitM < 0)
{
animator.SetBool("Shoot",true);
Instantiate(laser,new Vector2(transform.position.x-barrelLr,transform.position.y-barrelUd),Quaternion.identity);//transform.rotation=Quaternion.SetLookRotation(velocity);
waitM = 1;
}
}
}
``````

ProjectilePhysics:

``````public class ProjectilePhysics : MonoBehaviour {
//VECTORS
Vector2 mousePos;
Vector2 playerPos;
Vector2 forceDirection;
Vector2 distance;
//FLOATS
public float speed = 5;
float dir;
//GAMEOBJECTS
public GameObject player;
//BOOLS
bool hit = true;

// Use this for initialization
void Start () {
//GET MOUSE DIRECTION
mousePos = (Camera.main.ScreenToWorldPoint(Input.mousePosition));
playerPos = (player.transform.position);

forceDirection = mousePos - playerPos;
forceDirection.Normalize ();
if (mousePos.x > playerPos.x)
{
dir = 1;
}
else
{
dir = -1;
}
//END
//Debug.Log(dir);
//Debug.Log ("Mouse "+ mousePos.x);
//Debug.Log ("Player "+ playerPos.x);
Debug.Log (forceDirection);
}

//DETECT COLLISION
void OnTriggerExit2D(Collider2D other) {
hit = false;
}
//END

// Update is called once per frame
void Update () {

//DETERMINE DISTANCE FROM PLAYER
distance = transform.position - player.transform.position;
//END

//DESTROY OBJECT
if (distance.x > Screen.width / 2 || distance.x < -1 * (Screen.width / 2) || distance.y > Screen.height / 2 || distance.y < -1 * (Screen.height / 2)) {
Destroy (this.gameObject);
} else if (hit == false) {
Destroy (this.gameObject);
}
//END
}
void FixedUpdate(){
//MOVE PROJECTILE
//END
}
}
``````

Note: barrelUD and barrelLr are just offset values for the spawning location of the projectile so it doesn’t spawn in the players collider causing it to die.

You need to rotate the players gun towards the mouse position.

``````Vector 2 mousePos = Camera.main.ScreenToWorldPoint(input.mousePosition);

float neededRotation = Quaternion.LookRotation(transform.rotation - mousePos,Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, mousPos, rotationSpeed);
``````

So you probably need to make a script with this code inside it and add that script the socket that the projectile fires from. Or if you have a better way to implement it then go ahead.