- Home /
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.
Any ideas as to why this is occurring as I have hit a wall and cannot figure it out.
Answer by Graham-Dunnett · Feb 17, 2014 at 08:46 PM
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
![](https://koobas.hobune.stream/wayback/20220613133836im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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