I’ve tried a bunch of different ways but nothing seems to be working, I have a air plane that rotates based on where the mouse cursor is, now I just want the rate in which it rotates to slow down a bit to make a sort of “Star Fox” sort of control scheme.
public float targetOffset = 25.0f;
private Vector3 mTargetPoint;
private Vector3 mTarget;
private Vector3 currentPosition, previousPosition;
public float speed = 0.00000000100f;
Ray ray;
float t = 0.0f;
// Use this for initialization
void Start () {
currentPosition = Vector3.zero;
previousPosition = Vector3.zero;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
mTarget = ray.GetPoint(targetOffset);
}
// Update is called once per frame
void Update () {
float rate = 1.0F / speed;
float timeScale = 0.0F;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
mTarget = ray.GetPoint(targetOffset);
Transform newTransform = transform;
newTransform.LookAt(mTarget);
Quaternion start = transform.rotation; // ai start rotation
Quaternion dest = newTransform.transform.rotation; // some other rotation
/*
while (timeScale < rate)
{
timeScale += Time.deltaTime;
Quaternion start = transform.rotation; // ai start rotation
Quaternion dest = newTransform.transform.rotation; // some other rotation
transform.localRotation = Quaternion.Slerp(start, dest, timeScale / rate);
}*/
transform.localRotation = Quaternion.Lerp(start, dest, Time.deltaTime * speed);
}
I’ve used different values of “speed” and tried setting a time scale and slerp/lerp both and I don’t notice any difference.
lerp expects a value between 0-1 as the last parameter.
the idea being its returning a value that is a percentage between current and target. You are always providing a value thats most likely to be (for say a speed of 5), 5 * 0.01 (avg frame rate)… or 0.05… or 5%
The example code is quite silly — it would only work at the start of the game. At any other time, you would need to keep track of the startTime at which the rotation starts, and then subtract that from Time.time. And even then, it only works if you want to interpolate over exactly 1 second; for a different rotation period, you’d have to divide this difference by the rotation time.
Anyway… the way you’re trying to use Lerp is wrong, but it’s exactly correct for Quaternion.RotateTowards, which is what I recommend you use instead.
Strangely when I stop using the mouse and use the WASD keys I get something similar to Lerp/Slerp actually working, but now I’m just sort of baffled and confused at what to do.
I have a fighter plane I want to control with the mouse in a corridor/railshooter type of game, but with the mouse controlling the crosshairs and not the plane directly. I want the Mouse to move the crosshairs, and then the plane move in a “curve” across the screen to line up with it.
So move crosshair, the plane turns towards it (while the camera is still facing directly forward), as the plane moves closer to where the cross hair is, it then begins to “straighten out” until it reaches it.
I think most of my difficulties is that the Cross Hairs (currently a cube) I was “moving” with the plane (instead of scrolling the map I had the plane actually move with the map being stationary).
I think I’ll try to keep the Cross Hair’s “Z” position fixed by updating it’s position with the offset, dispense with keeping it offset by the radius of a “sphere” and try again with the Lerp/Slerp.
If that doesn’t work then I’ll try it with keeping the Plane fixed and proceed with a scrolling map implementation.
Advice/suggestions appreciated for a “Star Fox” control scheme.
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
direction = new Vector3(horizontal, vertical, 0);
finalDirection = new Vector3(horizontal, vertical, 1.0f);
transform.position += direction * flySpeed * Time.smoothDeltaTime;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(finalDirection), Mathf.Deg2Rad * 50.0f);
With VSYNC on there’s shakeyness/stutter. This seems fairly consistent for any sort of “Rotate Towards Some Target” implementation where the object also “moves”.