Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 11, 2016 at 01:32 PM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RossGames · Mar 07, 2016 at 04:42 PM · mouselookmathfnegativeinfinity

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Mar 07, 2016 at 04:52 PM 0
Share

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.

avatar image RossGames Owen-Reynolds · Mar 07, 2016 at 05:02 PM 0
Share

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.

avatar image $$anonymous$$ · Mar 07, 2016 at 06:43 PM 0
Share

What if you declare it as public float $$anonymous$$imalRotationX = -maximalRotationX?

2 Replies

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RossGames · Mar 07, 2016 at 07:11 PM 0
Share

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.

avatar image elenzil RossGames · Mar 08, 2016 at 12:55 AM 0
Share

nice! glad that was it. i can't tell you how many times this has bitten me.

avatar image Owen-Reynolds · Mar 08, 2016 at 01:43 AM 0
Share

$$anonymous$$oving this to the HelpRoom. $$anonymous$$isunderstandings like this are part of learning to use Unity.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · Mar 08, 2016 at 03:21 AM 1
Share

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".

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Let character moving towards it's facing? 0 Answers

How do I make my player look and make my bullet move at direction of my mouse 1 Answer

Rigidbody script throwing error CS0246: The type or namespace name `MouseLook' could not be found. Are you missing an assembly reference? 0 Answers

Predicting the hit point on X axis, based on vector direction 1 Answer

Player look at mouse via rotation on z axis 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges