Shooting towards mousePosition


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.


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.


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?


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


public class ProjectilePhysics : MonoBehaviour {
	Vector2 mousePos;
	Vector2 playerPos;
	Vector2 forceDirection;
	Vector2 distance;
	public float speed = 5;
	float dir;
	public GameObject player;
	bool hit = true;
	// Use this for initialization
	void Start () {
		mousePos = (Camera.main.ScreenToWorldPoint(Input.mousePosition));
		playerPos = (player.transform.position);

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

	void OnTriggerExit2D(Collider2D other) {
		hit = false;

	// Update is called once per frame
	void Update () {
		//rigidbody2D.AddForce (new Vector2 (dir, 0),ForceMode2D.Impulse);

		distance = transform.position - player.transform.position;

		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);
	void FixedUpdate(){
		rigidbody2D.AddForce (mousePos ,ForceMode2D.Impulse);

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.