Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Thunderball91 · Feb 17, 2014 at 08:44 PM · bugfloatunity4

Error with my float variable?

Hi all,

Basically I am making a feeding frenzy style game and have encountered a weird bug and can't figure out what is going on with it.

Upon eating a fish I add 0.2f to my float stored in the player script. After I reach a size around 5-7 the float goes from being 5 or 5.2 etc to 5.999999 and begins to screw up my animation which is set to change when the size is greater than the whole number. (which then in turn affects my collision detection etc)

My code for the Player(parent) is:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     // Animator references and Camera
     Animator anim;
     Eat eat;
     SpriteRenderer spr;
     BoxCollider2D pCollider;
 
     // Variables
     public bool facingRight = false;            // For determining which way the player is currently facing.
     public float Mspeed = 15f;
     public float pSize = 2.0f;
     private float MAXSIZE = 7.0f;
 
     // Sprite Size Variables
     public  float spriteX = 0f;
     public  float spriteY = 0f;
     public float spriteZ = 0f;
 
     // Use this for initialization
     void Start () 
     {
         anim = this.GetComponent<Animator>();
         spr = this.GetComponent<SpriteRenderer>();
         pCollider = this.GetComponent<BoxCollider2D>();
 
         spriteX = spr.sprite.bounds.size.x;
         spriteY = spr.sprite.bounds.size.y;
         spriteZ = spr.sprite.bounds.size.z;
     }
     
     // Update is called once per frame
     void Update () 
     {
         InputControl();
         SizeControl(pSize);
     }
 
     // Control the players movement
     void InputControl()
     {
         // Left
         if(Input.GetKey("a"))
         {
             rigidbody2D.AddForce(Vector2.right * -Mspeed * Time.fixedDeltaTime);
         }
         if(Input.GetKey("d"))
         {
             rigidbody2D.AddForce(Vector2.right * Mspeed * Time.fixedDeltaTime);
         }
         if(Input.GetKey("w"))
         {
             rigidbody2D.AddForce(Vector2.up * Mspeed * Time.fixedDeltaTime);
         }
         if(Input.GetKey("s"))
         {
             rigidbody2D.AddForce(Vector2.up * -Mspeed * Time.fixedDeltaTime);
         }
 
         // Cache the horizontal input.
         float h = Input.GetAxis("Horizontal");
         
         // If the input is moving the player right and the player is facing left...
         if(h > 0 && !facingRight)
         {
             // ... flip the player.
             Flip();
         }
         // Otherwise if the input is moving the player left and the player is facing right...
         else if(h < 0 && facingRight)
         {
             // ... flip the player.
             Flip();
         }
     }
 
     // Flip Sprite
     void Flip()
     {
         // Switch the way the player is labelled as facing.
         facingRight = !facingRight;
         
         // Multiply the player's x local scale by -1.
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
     
     // Change the size of the player using the animation 
     void SizeControl(float Size)
     {        
         // Max Size Restraint
         if(pSize > MAXSIZE)
         {
             pSize = MAXSIZE;
         }
         else if(pSize < 2)
         {
             pSize = 2;
         }
 
         // update our sprite sizes
         spriteX = spr.sprite.bounds.size.x;
         spriteY = spr.sprite.bounds.size.y;
         spriteZ = spr.sprite.bounds.size.z;
 
         // Set out Size float within the animator to our pSize var
         anim.SetFloat("Size", pSize);
         // Rescale our colliders for the player.
         pCollider.size = new Vector3(spriteX, spriteY, spriteZ);
     }
     
 }

My code for the Child object attached to the Player is:

 using UnityEngine;
 using System.Collections;
 
 public class Eat : MonoBehaviour {
 
     Player playerScript;
     BoxCollider2D pCollider;
 
     // box collider sizes these will stay the same
     float bbX1 = 0f;
     float bbX2 = 0f;
     //float bbY = 0f;
     //float bbZ = 0f;
 
     // Sprite Position
     float pPosX = 0f;
     float pPosY = 0f;
     float pPosZ = 0f;
     
     // Use this for initialization
     void Start () {
         playerScript = (Player)FindObjectOfType(typeof(Player));
         pCollider = (BoxCollider2D)GetComponent(typeof(BoxCollider2D));
     }
     
     // Update is called once per frame
     void Update () 
     {
         // Update our local copies of the player position
         pPosX = playerScript.transform.position.x;
         pPosY = playerScript.transform.position.y;
         pPosZ = playerScript.transform.position.z;
 
         // update our box sizes based upon the size of the sprite
         // spriteX stores the sprite in use's size
         bbX1 = -((playerScript.spriteX / 2) + -pPosX);
         bbX2 = ((playerScript.spriteX / 2) + pPosX);
 
         if(!playerScript.facingRight)
         {
             pCollider.transform.position = new Vector3(bbX1, pPosY, pPosZ);
         }
         else if(playerScript.facingRight)
         {
             pCollider.transform.position = new Vector3(bbX2, pPosY, pPosZ);
         }
     }
 
     void OnCollisionEnter2D (Collision2D col)
     {
         // If the colliding gameobject is an Enemy...
         if(col.gameObject.tag == "Angel_Fish")
         {
             DestroyObject(col.gameObject);
             playerScript.pSize += 0.2f;
         }
     }
 }
 

Also here is a screenshot of the variable output when running the game after the bug occurs.

alt text

Any ideas as to why this is occurring as I have hit a wall and cannot figure it out.

bug.png (8.3 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Graham-Dunnett · Feb 17, 2014 at 08:46 PM

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

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 Thunderball91 · Feb 17, 2014 at 10:07 PM 0
Share

Thanks, after reading that I changed it to a double variable type ins$$anonymous$$d of a float and the issue has been fixed. Thanks for your time.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

Unity4 Ignoring Inspector/Initialized Public Value in Build 1 Answer

Dynamic Font On Android Can't display some texts 4 Answers

Why does fmod(x,1.0) not work in Unity4, but fmod(x,1.0001) does? 0 Answers

Rotation Values Garbled When Set By Code 2 Answers

transform.position = new Vector 3 NOT moving to correct position? 1 Answer


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