I have problems with a character. I want to move myself at some speed, and by typing on WASD keys to rotate this object and move to the side where the face is. The object moves continuously without pressing the key. Let's say something to the snake game.This is my code, it goes, but I can not do this rotation.
private float speed = 0.01f;
private float moveSpeed = 12f;
Vector2 vector = Vector2.up;
Vector2 moveVector;
[HideInInspector] public bool IsMoving = false;
[HideInInspector] public bool canPress = true;
private void Start()
{
InvokeRepeating("Movement", 0.5f, speed);
}
void Update()
{
if (canPress)
{
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
canPress = false;
IsMoving = true;
vector = Vector2.right;
//transform.Rotate(0f, 0f, -90f);
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + rotateObj, transform.rotation.z);
}
else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
canPress = false;
IsMoving = true;
vector = Vector2.up;
}
else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
canPress = false;
IsMoving = true;
vector = -Vector2.up;
//transform.Rotate(0f, 0f, -90f);
}
else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
canPress = false;
IsMoving = true;
vector = -Vector2.right;
//transform.Rotate(0f, 0f, +90f);
}
}
moveVector = vector / moveSpeed;
}
void Movement()
{
if (IsMoving)
transform.Translate(moveVector);
}
If anyone knows how to do this I would be grateful. !!!