In my code I have the player moving towards the mouse position when I press W and from the mouse position when pressing S. I also want the player to move left and right when I press A and D. However, I don’t want the player to move left and right according to the X and Y-plane but relative to where the player is to the mouse position (the player rotates towards the mouse position at all times). So basically, when the player is rotated I want it to move perpendicular to it’s direction to the mouse position when I press A or D.
It’s not really working. I think you made a mistake to begin with, since you never actually calculate the vector between the mouse and the object’s position. I redid the code a bit and got this:
It works as long as the player is moving along the x and y-axis. However, if I’m moving 45 degrees (my player always follows the mouse position) it’s not working. Then the player just continues to walk towards/from the mouse if I press A or D.
You also forgot what I had mentioned above. You have to not only switch, but also invert either the x or the y value.
I’ve written a quick example, which at least uses the correct perpendicular (clockwise) vector on a per-frame basis. If you want to eliminate the “rotate around” effect when you move sideways , you can do that of course.
using UnityEngine;
public class CLASS_NAME : MonoBehaviour
{
private Camera _mainCamera;
[SerializeField]
private float speed = 2f;
private void Awake()
{
_mainCamera = Camera.main;
}
private void Update()
{
var mouseWorldPosition = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
mouseWorldPosition.z = 0f; // this should actually be transform.position.z in case your sprite is not located at z = 0
// this is one way to rotate, right (x-axis) being the new forward
transform.right = (mouseWorldPosition - transform.position);
var movementVector = GetMovementVector();
transform.Translate(movementVector * Time.deltaTime * speed);
}
private Vector2 GetMovementVector()
{
Vector2 movementDirection = Vector2.zero;
// accumulates the input, use else if to only enable one key at a time
if (Input.GetKey(KeyCode.A))
{
movementDirection.y -= 1f;
}
if (Input.GetKey(KeyCode.D))
{
movementDirection.y += 1f;
}
if (Input.GetKey(KeyCode.W))
{
movementDirection.x += 1f;
}
if (Input.GetKey(KeyCode.S))
{
movementDirection.x -= 1f;
}
return movementDirection.normalized;
}
}