I want to make my sprite move up, down, left, and right, but never diagonal. Also, I wanted to make it with translate, and not with a rigid body. So far, this is the code I have.
`using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
[SerializeField] float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Move();
SpriteRotate();
}
private void Move()
{
Vector3 movement= new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0f);
transform.position += movement * moveSpeed * Time.deltaTime;
}
private void SpriteRotate()
{
if (Input.GetKeyDown(KeyCode.W))
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.eulerAngles = new Vector3(0, 0, 180);
}
else if (Input.GetKeyDown(KeyCode.A))
{
transform.eulerAngles = new Vector3(0, 0, 90);
}
else if (Input.GetKeyDown(KeyCode.D))
{
transform.eulerAngles = new Vector3(0, 0, -90);
}
}
}`