c# – How to manage movement on a topdown shooter

I’m attempting to code a top down shooter in c#, I’ve worked out how to get my character to look at the mouse, and got it to be able to move in all 4 directions, but how do i make it go forwards towards the mouse when I press w and back away from the mouse when i press S etc, much thanks. Code attached

    var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
    var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email