Why is my character strafing around the mouse (2d)

The way I understand it, my character SHOULD be translating +X, -X, +Y, -Y, but instead it’s translating “left around mouse cursor” etc. I have no idea what happened, the code is simple enough.

Any tips? ideas?
(I know my bullet code is broken, I’m trying to fix that one before coming asking for help)

Thanks

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
	public int spd;
	public GameObject bullet;
	GameObject firedBullet;
	Vector3 mousePos;
	Vector3 worldPos;
	float angle;

	// Use this for initialization
	void Start () {

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




		Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);



		if(Input.GetKey (KeyCode.W))
		{

			transform.Translate (0, (spd * Time.deltaTime), 0);

		}
		if(Input.GetKey (KeyCode.S))
		{

			transform.Translate (0, 0 - (spd * Time.deltaTime), 0);
		}
		if(Input.GetKey (KeyCode.A))
		{

			transform.Translate ( 0 - (spd * Time.deltaTime), 0, 0);
		}
		if(Input.GetKey (KeyCode.D))
		{

			transform.Translate ((spd * Time.deltaTime),0 , 0);
		}
		if(Input.GetButtonDown ("Fire1"))
		{
			firedBullet = Instantiate(bullet, transform.position, Quaternion.AngleAxis (90, Vector3.up) ) as GameObject;
		}
			
	}
}

–EDIT-- Removed previous answer and replaced with this, so for anybody reading… the comments might not make sense.

Ok, after looking at your project I see what you mean and I have found your problem.

The following line

transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position); 

will always make your cube look at the mouse position.

The transform.Translate() function is movement relative to it’s current position and local orientation.

So this means every time you move left and right, and the next time it updates the cube will look back at the mouse giving it a new direction to translate with, causing it to orbit.

If you don’t want it to face the mouse when strafing, add that line of code to the if statements on W and S instead of it being where it is now. Doing this will only make the cube look at the mouse when moving forwards and backwards (assuming this is what you want).

But if you want it to just move around without influence of the mouse position and still rotate, you need to use a different method of moving your cube. There are a few. You could set the position of the cube manually… like this:

[old] transform.Translate (0, (spd * Time.deltaTime), 0);
[new] transform.position += new Vector3(0, spd * Time.deltaTime, 0);