Code:
I’m sure the error is within this part, because when I change both returns to true, then it works like a charm, resetting the game every frame.
I’ve also tried currentHandle.transform.rotation.Equals(0) != true for the conditional
and currentHandle.transform.rotation.z != 0.
AND currentHandle.transform.rotation != Quaternion.Identity
AND ALSO currentHandle.transform.rotation != Quaternion.EulerAngles(0, 0, 0)
You get the point…
Help would be most appreciated!
Never test floating point (float) quantities for equality / inequality. Here’s why:
https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html
Now that you have had your coffee, don't forget about floating point imprecision:
// @kurtdekker: don't forget about floating point imprecision
using UnityEngine;
public class FloatingPointImprecision : MonoBehaviour
{
void Start ()
{
float a = 2000.0f;
float b = 0.1f;
float product = (a * b);
float answer = 200;
Debug.Log( "a = " + a);
Debug.Log( "b = " + b);
Debug.Log( "product of a*b = " + product);
Debug.Log( "answer …
This will explain it all.
In an IEEE single-floating point number you can represent certain numbers perfectly and unambiguously.
For instance, 0.5f is precisely represented as 0x3f000000
However, 0.1f CANNOT be precisely represented. One possible approximate representation is 0x3DCCCCCD
The analogy is that you cannot show 1/3rd precisely as a decimal.
0.33 is correct to 2 decimal places
0.3333 is correct to 4 places
etc.
But you can never exactly represent 1/3rd as a decimal numeral. Yo…
“Think of [floating point] as JPEG of numbers.” - orionsyndrome on the Unity3D Forums
Literal float / double issues:
Welcome! In this forum, you will get much more help if you format your code with code tags <= LINK.
Your error message actually tells you what the problem is, and how to fix it. Let's break it down.
First, it tells you where the problem is:
Assets\PlayerMovement.cs(12,49): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
It's happening at Line 12, Column 49. Here's Line 12:
[SerializeField] private f…
And thanks to halley for this handy little visual floating point converter:
https://www.h-schmidt.net/FloatConverter/IEEE754.html
As an alternative to trying to compare exactly:
bool closeEnough = Mathf.Abs(a - b) < margin;
Set “margin” to some reasonably small value. As long as a and b are no further apart than that value, the result will be true.
Note that there is Mathf.Approximate
, but that only gives you a very, very small margin (the epsilon value). That’s probably not appropriate if you’re checking if a handle is in the right position!
Still not working. Here’s my program:
using UnityEngine;
using TMPro;
public class AllClocks : MonoBehaviour
{
// All Clocks
public GameObject[ ] longHandles = new GameObject[9];
public TextMeshProUGUI WinText;
public GameObject Button;
// Winstate stuff
private bool CheckWinState()
{
for (int i = 0; i < longHandles.Length; i++)
{
// Resetting Values
bool rotationMoreThanNegMargin;
bool rotationLessThanMargin;
// Initializing
GameObject currentHandle = longHandles*;*
//Variables
int margin = 5;
int negMargin = margin * -1;
//If the rotation is more than -5.
if (currentHandle.transform.rotation.z > negMargin)
{
rotationMoreThanNegMargin = true;
}
else
{
rotationMoreThanNegMargin = false;
}
//If the rotation is less than 5.
if (currentHandle.transform.rotation.z < margin)
{
rotationLessThanMargin = true;
}
else
{
rotationLessThanMargin = false;
}
if (rotationLessThanMargin == true && rotationMoreThanNegMargin == true)
{
return false;
}
}
return true;
}
private int clockIndex = 100;
KeyCode[ ] INPUT_REFERENCES = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9};
private void Start()
{
RandomizeClockRotations();
}
private void Update()
{
if (CheckWinState())
{
EndGame();
}
for (int i = 0; i < INPUT_REFERENCES.Length; i++)
{
if (Input.GetKey(INPUT_REFERENCES*))
_ {_
clockIndex = i + 1;
_ }_
_ }_
if (Input.GetKeyDown(KeyCode.Alpha0))
_ {_
for (int i = 0; i < longHandles.Length; i++)
_ {_
if (clockIndex <= 9 && clockIndex >= 0)
_ {_
if (i == clockIndex - 1)
_ {_
_longHandles .transform.Rotate(Vector3.back, 60);
}
else
{_
_longHandles .transform.Rotate(Vector3.forward, 60);
}
}
}
}
}
private void RandomizeClockRotations()
{
for (int i = 0; i < longHandles.Length; i++)
{
int initialRandomNumber = Random.Range(0, 3); _
int rotationRandomnumber = 120 * initialRandomNumber;
_longHandles .transform.Rotate(Vector3.forward, rotationRandomnumber);
}
}
private void EndGame()
{
enabled = false;
WinText.text = “You Win!”;
Button.SetActive(true);
}
public void ResetState()
{
enabled = true;
RandomizeClockRotations();
WinText.text = “”;
Button.SetActive(false);
clockIndex = 100;
}
}*_
Be aware that the values you read in the inspector for transform and rotation values are their local values, and in particular with rotation it’s a ‘readable’ version of the actual rotation values going under the hood.
Start Debug.Log()-ing to see what numbers are being read off the objects eular angles and where your code is going wrong. This should have been your first step in general, otherwise you’re shooting in the dark.
Also use code tags next time, please. There’s instructions stickied to this subforum.
using UnityEngine;
using TMPro;
public class AllClocks : MonoBehaviour
{
// All Clocks
public GameObject[] longHandles = new GameObject[9];
public TextMeshProUGUI WinText;
public GameObject Button;
// Winstate stuff
private bool CheckWinState()
{
for (int i = 0; i < longHandles.Length; i++)
{
// Resetting Values
bool rotationMoreThanNegMargin;
bool rotationLessThanMargin;
// Initializing
GameObject currentHandle = longHandles[i];
//Variables
int margin = 5;
int negMargin = margin * -1;
//If the rotation is more than -5.
if (currentHandle.transform.rotation.z > negMargin)
{
rotationMoreThanNegMargin = true;
}
else
{
rotationMoreThanNegMargin = false;
}
//If the rotation is less than 5.
if (currentHandle.transform.rotation.z < margin)
{
rotationLessThanMargin = true;
}
else
{
rotationLessThanMargin = false;
}
if (rotationLessThanMargin == true && rotationMoreThanNegMargin == true)
{
return false;
}
}
return true;
}
private int clockIndex = 100;
KeyCode[] INPUT_REFERENCES = {KeyCode.Alpha1, KeyCode.Alpha2, KeyCode.Alpha3, KeyCode.Alpha4, KeyCode.Alpha5, KeyCode.Alpha6, KeyCode.Alpha7, KeyCode.Alpha8, KeyCode.Alpha9};
private void Start()
{
RandomizeClockRotations();
}
private void Update()
{
if (CheckWinState())
{
EndGame();
}
for (int i = 0; i < INPUT_REFERENCES.Length; i++)
{
if (Input.GetKey(INPUT_REFERENCES[i]))
{
clockIndex = i + 1;
}
}
if (Input.GetKeyDown(KeyCode.Alpha0))
{
for (int i = 0; i < longHandles.Length; i++)
{
if (clockIndex <= 9 && clockIndex >= 0)
{
if (i == clockIndex - 1)
{
longHandles[i].transform.Rotate(Vector3.back, 60);
}
else
{
longHandles[i].transform.Rotate(Vector3.forward, 60);
}
}
}
}
}
private void RandomizeClockRotations()
{
for (int i = 0; i < longHandles.Length; i++)
{
int initialRandomNumber = Random.Range(0, 3);
int rotationRandomnumber = 120 * initialRandomNumber;
longHandles[i].transform.Rotate(Vector3.forward, rotationRandomnumber);
}
}
private void EndGame()
{
enabled = false;
WinText.text = "You Win!";
Button.SetActive(true);
}
public void ResetState()
{
enabled = true;
RandomizeClockRotations();
WinText.text = "";
Button.SetActive(false);
clockIndex = 100;
}
}