Hello trying to make a topdown game with WASD for movement and the player turning to follow mouse. I have the lookatmouse script working but now my movement when my character looks to the right on screen pressing “a” to go left moves character left not screen left. How do I make it ignore the direction my character is facing?
public class LookAtMouse : MonoBehaviour
{
private Camera cam;
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
Vector3 mousePos = (Vector2)cam.ScreenToWorldPoint(Input.mousePosition);
float angleRad = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x);
float angleDeg = (180 / Mathf.PI) * angleRad - 90;
transform.rotation = Quaternion.Euler(0f, 0f, angleDeg);
}
}
public class PlayerControllerAlternate : MonoBehaviour
{
float playerSpeed = 1.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector2.up * playerSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector2.down * playerSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector2.left * playerSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * playerSpeed * Time.deltaTime);
}
}
}