The question is answered, right answer was accepted
Mathf.NegativeInfinity unexpectly converts to PositiveInfinity
Hello, basically, I have a mouse-look script, where you can set the limits of view (e.g. you can't circle vertically). But I want to add a horizontal limit too, which is, by default, infinity. And I ran into a problem, when I initialize a minimum X rotation limit to negative infinity (that shows correctly in the inspector), in Awake function, it prints positive infinity.
Here is my code:
using UnityEngine; using System.Collections;
[AddComponentMenu("Camera-Control/Mouse-Look")]
public class PlayerCameraController : MonoBehaviour {
[Space(-10)]
[Header("Sensitivity")]
[Space()]
public float sensitivityX = 15f;
public float sensitivityY = 15f;
[Header("Rotation limits", order=3)]
[Space(-10, order=4)]
[Header(" X", order=5)]
[Space(order=6)]
public float maximalRotationX = Mathf.Infinity;
public float minimalRotationX = Mathf.NegativeInfinity;
[Space(-10, order=7)]
[Header(" Y", order=8)]
[Space(order=9)]
public float maximalRotationY = 90f;
public float minimalRotationY = -90f;
private float oldX;
private float oldY;
private Quaternion originalRotation;
void Awake () {
float a = Mathf.NegativeInfinity;
print (a);
originalRotation = transform.localRotation;
oldX = originalRotation.x;
oldY = originalRotation.y;
}
void UpdateRotation () {
float rX = Mathf.Clamp (Input.GetAxis ("Mouse X") * sensitivityX, -360f, 360f) + oldX;
float rY = Mathf.Clamp (Input.GetAxis ("Mouse Y") * sensitivityY, -360f, 360f) + oldY;
rX = rX <= maximalRotationX && rX >= minimalRotationX ? rX : oldX;
rY = rY <= maximalRotationY && rY >= minimalRotationY ? rY : oldY;
Quaternion qX = Quaternion.AngleAxis (rX, Vector3.up);
Quaternion qY = Quaternion.AngleAxis (rY, Vector3.left);
transform.localRotation = qX * qY;
oldX = rX;
oldY = rY;
}
void FixedUpdate () {
UpdateRotation ();
}
}
Any ideas?
For fun, what does if(a<0)
tell you? Does the program still work? The infinity stuff seems like C# issues. You might just read up issues with them in general (not in Unity.)
And, of course, -99999 should work.
That's it. $$anonymous$$y rotation limits work on checking rotationToPerform < maximalRotationX
etc. and it doesn't work. When I declare a local variable as NegativeInfinity, it stays at its value. I can use something like -999999, but that's not-so obvious to read that it's unlimited. And I want to discover where the bug was, because I want to learn.
What if you declare it as public float $$anonymous$$imalRotationX = -maximalRotationX
?
Answer by elenzil · Mar 07, 2016 at 06:36 PM
so, when you run this, the value for "a" prints as regular positive Infinity ? or is "minimalRotationX" becoming positive ?
if that's the case, you might want to double-check what the value is in the scene. any of these public variables are actually initialized from values in the scene, not from code.
Oh, God. You're right, in the scene, I had the $$anonymous$$imal limit set to positive infinity. I have reset only the prefab's component, because I tough that when you change the prefab, the correspondenting scene game objects will be updated too. Thank you, you can make an answer from this.
nice! glad that was it. i can't tell you how many times this has bitten me.
$$anonymous$$oving this to the HelpRoom. $$anonymous$$isunderstandings like this are part of learning to use Unity.
Answer by djpiper28 · Mar 07, 2016 at 08:46 PM
Try the code below
function update(){ if(/*Variable name*/<=0) { var /*Variable name*/= /*Variable name*/ * -1} }
This is for java script though for C# try:
void update(){ if(/*Variable name*/<=0) { float /*Variable name*/= /*Variable name*/ * -1} }
This will multiple the variable with a positive infinity if it goes negative.
Please make sure you format your text and code properly. The answer field gives you a preview of what it will look like.
You can edit your answers and comments by clicking the little cog wheel and selecting "Edit".