Hi there, new unity user here.
I am making a basic top down shooter game and I’m trying to have my sprite face my mouse. I’m using a basic movement which works fine on its own, but I’m trying to have my sprite face the direction of my mouse (which also works fine on its own.)
When putting these two together, it makes my movement all wonky, and I feel I’m overlooking something very simple. I’m just wondering if there is a way to make my movement independent of the way my sprite is facing.
Here is the code
void Update() {
//Movement WIP
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
}
//Mouse Look WIP
var mouse = Input.mousePosition;
var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y);
var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
}