Question by
Monsterwald · Jun 17, 2021 at 08:03 AM ·
c#floatprecision
Increase number whenever object makes a whole rotation
I have a key and as long I hold this key, my object rotates. Whenever my object makes a whole ration, the value "int rotationAmount" should increase by one. I wrote a code and it works but only on low speed, if the speed gets too high it wont work due to imprecision. How can I make it work at high speed?
public class Controlltest : MonoBehaviour
{
[SerializeField] float spinSpeed;
int rotationAmount;
float rotationAngle;
float rotationValue;
private void Update()
{
if (Input.GetKey(KeyCode.A))
{
rotationValue += (Time.deltaTime * spinSpeed)/360;
rotationAngle = (Mathf.Round(Mathf.Abs(rotationValue) * 100) / 100);
transform.Rotate(0, 0, spinSpeed * Time.deltaTime);
}
if (rotationAngle == 1)
{
rotationAmount++;
rotationValue = 0;
}
}
private void OnGUI()
{
GUI.Box(new Rect(10, 10, 150, 30), "Angle: " + rotationAngle);
GUI.Box(new Rect(10, 50, 150, 30), "Rotation: " + rotationAmount);
}
}
Comment
Your answer
Follow this Question
Related Questions
Reference problem 1 Answer
float value carries over from runtime causing issues + it doesnt update in the inspector 0 Answers
[SOLVED!] PlayerPrefabs SetFloat and GetFloat is not working 0 Answers
[Netcode] exponential floats notation causes a addition error 0 Answers
Can you do float + float 1 Answer