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);
  • \$\begingroup\$ It looks like you're using Unity. Presumably you tried using the built-in transform.forward and transform.right vectors to compute your displacement/velocity in each direction? Or using transform.rotation to rotate a local space displacement to match the player character's orientation in world space? \$\endgroup\$
    – DMGregory
    Nov 20 '21 at 18:46

You must log in to answer this question.

Browse other questions tagged .