- Home /
How to create smooth object rotation in a WASD system?
I am trying to create a control scheme wherein the player controlled element can be rotated by the player using the WASD keys, but am having trouble creating a programmed scheme that doesn't stutter constantly or just barely move at all before returning to where it started. I need a system that rotates relative to the current rotation of the object, but also maintains rotation after each iteration of the code, or just a simpler coding solution.
This is my current code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public bool isMovingX;
public bool isMovingY;
public float movementX;
public float movementY;
public Quaternion rotationX1;
public Quaternion rotationX2;
public Quaternion rotationY1;
public Quaternion rotationY2;
private float timeCountX;
private float timeCountY;
void Start()
{
movementX = 3;
movementY = 3;
timeCountX = 3f;
timeCountY = 3f;
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W) == true && Input.GetKey(KeyCode.S) == false && isMovingX == false)
{
timeCountX = movementX * Time.deltaTime;
rotationX1 = transform.rotation;
rotationX2 = Quaternion.Euler(transform.rotation.X - 5, transform.rotation.y + 0, transform.rotation.z + 0);
transform.localRotation = Quaternion.Slerp(rotationX1, rotationX2, timeCountX);
isMovingX = true;
StartCoroutine(WaitTimeX());
}
if (Input.GetKey(KeyCode.A) == true && Input.GetKey(KeyCode.D) == false && isMovingY == false)
{
timeCountY = movementY * Time.deltaTime;
rotationY1 = Quaternion.Euler(transform.rotation.X, transform.rotation.Y, transform.rotation.Z);
rotationY2 = Quaternion.Euler(transform.rotation.X + 0, transform.rotation.y - 5, transform.rotation.z + 0);
transform.localRotation = Quaternion.Slerp(rotationY1, rotationY2, timeCountY);
isMovingY = true;
StartCoroutine(WaitTimeY());
}
if (Input.GetKey(KeyCode.S) == true && Input.GetKey(KeyCode.W) == false && isMovingX == false)
{
timeCountX = movementX * Time.deltaTime;
rotationX1 = Quaternion.Euler(transform.rotation.X, transform.rotation.Y, transform.rotation.Z);
rotationX2 = Quaternion.Euler(transform.rotation.X + 5, transform.rotation.y + 0, transform.rotation.z + 0);
transform.localRotation = Quaternion.Slerp(rotationX1, rotationX2, timeCountX);
isMovingX = true;
StartCoroutine(WaitTimeX());
}
if (Input.GetKey(KeyCode.D) == true && Input.GetKey(KeyCode.A) == false && isMovingY == false)
{
timeCountY = movementY * Time.deltaTime;
rotationY1 = Quaternion.Euler(transform.rotation.X, transform.rotation.Y, transform.rotation.Z);
rotationY2 = Quaternion.Euler(transform.rotation.X + 0, transform.rotation.y + 5, transform.rotation.z + 0);
transform.localRotation = Quaternion.Slerp(rotationY1, rotationY2, timeCountY);
isMovingY = true;
StartCoroutine(WaitTimeY());
}
if (Input.GetKey(KeyCode.W) == false && Input.GetKey(KeyCode.S) == false)
{
movementX = 5;
}
if (Input.GetKey(KeyCode.A) == false && Input.GetKey(KeyCode.D) == false)
{
movementY = 5;
}
}
public IEnumerator WaitTimeX()
{
yield return new WaitForSeconds(0.1f);
if (movementX > 1)
{
movementX = movementX - 0.5f;
}
isMovingX = false;
}
public IEnumerator WaitTimeY()
{
yield return new WaitForSeconds(0.1f);
if (movementY > 1)
{
movementY = movementY - 0.5f;
}
isMovingY = false;
}
}
Comment