For those who have played the game, I’m trying to create the game so the player is always moving forward and the only controls are ‘A’ and ‘D’ to rotate the player so you can fly instead of going straight so just how to rotate the player with those two keys and how to keep the player moving at all times.
its untested, and I am sure there is probably an error in there someplace, but the math and logic is sound for what you are trying to accomplish.
// in your class, you should keep some values handy
public float speed = 10; // how fast the plane moves
public float maxRotationAngle = 45; // how much the plane can rotate
public float maxRotationAmount = 60; // how much rotation that is applied at max angle
private float angle = 0; // the current angle
private float howLongToMaxAngle = 0.5f; // how many seconds it takes to reach the max angle from zero
// in your update....
// first, get your keys
float h = Input.GetAxis("Horizontal");
// now, lets calcualte the angle needed.
float targetAngle = h * maxRotationAngle;
// now, lets get the local angles of the plane
Vector3 euler = transform.localEulerAngles;
// now we are going to rotate the euler's z angle to control roll over time.
euler.z = Mathf.lerp(euler.z, targetAngle, maxRotationAngle / howLongToMaxAngle * Time.deltaTime);
// set the euler back to the plane
transform.localEulerAngles = euler;
// get the current angle amount so we can figure out how much to turn at this angle
float currentRotate = euler.z / maxRotationAngle;
// multiply that by maxRotationAmount
currentRotate *= maxRotationAmount;
// rotate the plane by that amount over time in world space
transform.Rotate(Vector3.up * currentRotate * Time.deltaTime, Space.World);
// now lets move the plane forward by its speed over time.
transform.Translate(Vector3.forward * speed * Time.deltaTime);
1 Like
Thanks a lot. I can work with this and hopefully learn a bit from it in the process. Cheers.