using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour
{
public float speed;
public float RotationSpeed = 1;
public float RotationAcceleration = 1;
private float currentRotationSpeed;
// Start is called before the first frame update
void Start()
{
currentRotationSpeed = RotationSpeed;
}
// Update is called once per frame
void Update()
{
transform.Rotate(0, 0, speed * Time.fixedDeltaTime, Space.Self);
if (Input.GetKeyDown("w") || Input.GetKeyDown("s"))
{
currentRotationSpeed = RotationSpeed;
}
if (Input.GetKey("w"))
{
transform.Rotate(0, 0, currentRotationSpeed, Space.Self);
currentRotationSpeed = Time.fixedDeltaTime * RotationAcceleration;
}
if (Input.GetKey("s"))
{
transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
currentRotationSpeed = Time.fixedDeltaTime * RotationAcceleration;
}
}
}
I tried to remove this line : the line is just for testing automatic rotation.
transform.Rotate(0, 0, speed * Time.fixedDeltaTime, Space.Self);
in any case when i'm pressing the w key without leaving the key it's rotating and the speed is increasing but at some point the speed slow down and then before stop it's changing the rotation direction and start increasing the rotation speed again.
i want that when pressing the w key increase the speed up nonstop then when pressing the s key decrease the speed rotation from the current point slowly down to stop.
and then when pressing w it will start increasing the speed again and s slow down to stop smooth.
CodePudding user response:
As you increase the speed you eventually reach a point where you are applying more than 180 degrees of rotation per frame, at this point for every increase in speed the object will appear to slow down. When you reach 360 degrees of rotation per frame it will appear staionary and then star to slowly increase again.
What you are experiencing is very similar in principle to the stroboscopic effect when watching a spinning wheel in a flickering light, such as a fluorescent tube.
What you need to do is ensure that the speed never reaches the point where you are rotating the object more than 180 degrees per frame.
In other words you need to add some code that checks if(speed * Time.fixedDeltaTime < 180) and if it is to reduce it accordingly. Actually even that may be insufficient, you probably want to limit the step to 45 degrees to preserve the semblance of rotation.