Hello
I am working on 2D top down shooter game.
I used the transform.translate in my movement script, and it worked great, so the movement would go in the direction where the player was facing.
So if i pressed the “up” key and the player was looking down, then he would move down. Basiclly where ever the mouse pointe was as the player would look at that direction.
But i could not figure out how to use the normalize so i had constant speed also in the diagonal movement. So i change my movememt script, that fixed the normalize move but now if i press “up” key, even though i am looking down, the plater will move up.
Here below the new code:
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
moveDirection = new Vector2(moveX, moveY).normalized;
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
var speed = Mathf.Abs(moveDirection.x) + Mathf.Abs(moveDirection.y);
And here the old code:
//horizontal = Input.GetAxisRaw("Horizontal");
//vertical = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector2(horizontal * moveSpeed, vertical * moveSpeed);
transform.Translate(Vector2.left * Time.deltaTime * moveSpeed * horizontal);
transform.Translate(Vector2.down * Time.deltaTime * moveSpeed * vertical);
var speed = Mathf.Abs(moveDirection.x) + Mathf.Abs(moveDirection.y);