My character rotates 90° to the left when the Q key is pressed, and vice versa when the E key is pressed. However, this happens instantly without smooth movement. I saw lots of questions, but they were off-topic. I heard you can do this with “Time.deltaTime
”.
My code is here:
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
public static int speed = 5;
private Vector3 endpos;
private bool moving = false;
void Start () {
endpos = transform.position;
}
void Update () {
if (moving && (transform.position == endpos))
moving = false;
if(!moving && Input.GetKey(KeyCode.W)){
moving = true;
endpos = transform.position + transform.forward;
}
if(!moving && Input.GetKey(KeyCode.A)){
moving = true;
endpos = transform.position - transform.right;
}
if(!moving && Input.GetKey(KeyCode.S)){
moving = true;
endpos = transform.position - transform.forward;
}
if(!moving && Input.GetKey(KeyCode.D)){
moving = true;
endpos = transform.position + transform.right;
}
if(!moving && Input.GetKey(KeyCode.Q)){
moving = true;
transform.Rotate(0,-90,0);
}
if(!moving && Input.GetKey(KeyCode.E)){
moving = true;
transform.Rotate(0,90,0);
}
transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * speed);
}
}
Thanks in advance!