0
votes

I'm making a top down 2D game in unity in C#. Currently just getting movement set up, but I have already hit a problem that I can't figure out. The game is set up in a grid and I'm just using the arrow keys to move up, down, left and right. This is my script:

public class Player : MonoBehaviour 
{

    public float playerSpeed;

    void FixedUpdate()
    {
        // Movement
        if (transform.position.x < 0.25)
        {
            if (Input.GetKeyUp(KeyCode.RightArrow))
            {
                transform.position += new Vector3(playerSpeed, 0, 0);
            }         
        }
        if (transform.position.x > -0.3)
        {
            if (Input.GetKeyUp(KeyCode.LeftArrow))
            {
                transform.position += new Vector3(-playerSpeed, 0, 0);
            }
        }
        if (transform.position.y < 0.15)
        {
            if (Input.GetKeyUp(KeyCode.UpArrow))
            {
                transform.position += new Vector3(0, playerSpeed, 0);
            }
        }
        if (transform.position.y > -0.10)
        {
            if (Input.GetKeyUp(KeyCode.DownArrow))
            {
                transform.position += new Vector3(0, -playerSpeed, 0);
            }
        }
    }
}

The first if statements for each direction is making sure the player doesn't leave the boundaries of the room. The annoying thing that is happening is when moving, it starts off in nice square numbers as intended like 0.05, 0.1, 0.15, etc, but then goes off course somehow and gives numbers like -0.05000001, -7.450581e-09, 0.04999999, which are close to the wanted numbers but not accurate enough. Any ideas? All suggestions appreciated.

2
isn't it due to the precision of the float? I mean, 0.0f is it equals to 0.000000001f ? - Ceros
would a double be better? - oscaro
Probably you would be better of making your grid using integers. Also, for this Update would do. - Gunnar B.
@oscaro That might because unity's Physics. But why do you mind that? - DRKblade
@DRKblade got a little annoying because of the stupid and inefficient way I was handling collisions and stuff. Going to change it to make it not stupid. - oscaro

2 Answers

0
votes
static void Main(string[] args)
{
  float wMyFloat = 1.5f;
  for(int i = 0; i < 100; i++)
  {
    wMyFloat += 0.1f;
  }


  Console.WriteLine(wMyFloat.ToString());
  Console.ReadLine();
}

You would except this to equal 11.5 but the print tells you that it is equal to 11.50001

By += float with float (or double), you will end up with an offset. And you wont be able to perform == also. 11.5 != 11.50001

0
votes
void FixedUpdate(){
        // Check to see if bounds left right
        if(transform.position.x < 0.25f && tranform.position.x > -0.3f){
            if (Input.GetKeyUp(KeyCode.RightArrow))
            {
                transform.position += new Vector3(playerSpeed, 0, 0);
            }    

            else if (Input.GetKeyUp(KeyCode.LeftArrow))
            {
                transform.position += new Vector3(-playerSpeed, 0, 0);
            }
        }
        // Check to see if bounds up and down
        if(transform.position.y < 0.15f && tranform.position.y > -0.1f){
            if (Input.GetKeyUp(KeyCode.UpArrow))
            {
                transform.position += new Vector3(0, playerSpeed, 0);
            }

            else if (Input.GetKeyUp(KeyCode.DownArrow))
            {
                transform.position += new Vector3(0, -playerSpeed, 0);
            }

        }
    }