1

I currently have code to move my "Player" around a surface using arrow keys, but I find the cube wiggles and doesn't move perfectly. So after a couple movements the coordinates are never perfect. Always something like 90.0012 or something related when I wanted it to be 90 per say. anyone have any suggestions? here's the code:

using System.Collections;
using UnityEngine;

public class TumblingCubes : MonoBehaviour
{

    public float tumblingDuration = 0.2f;

    void Update()
    {
        var dir = Vector3.zero;

        if (Input.GetKey(KeyCode.UpArrow))
            dir = Vector3.forward;

        if (Input.GetKey(KeyCode.DownArrow))
            dir = Vector3.back;

        if (Input.GetKey(KeyCode.LeftArrow))
            dir = Vector3.left;

        if (Input.GetKey(KeyCode.RightArrow))
            dir = Vector3.right;

        if (dir != Vector3.zero && !isTumbling)
        {
            StartCoroutine(Tumble(dir));
        }
    }

    bool isTumbling = false;
    IEnumerator Tumble(Vector3 direction)
    {
        isTumbling = true;

        var rotAxis = Vector3.Cross(Vector3.up, direction);
        var pivot = (transform.position + Vector3.down * 0.5f) + direction * 0.5f;

        var startRotation = transform.rotation;
        var endRotation = Quaternion.AngleAxis(90.0f, rotAxis) * startRotation;

        var startPosition = transform.position;
        var endPosition = transform.position + direction;

        var rotSpeed = 90.0f / tumblingDuration;
        var t = 0.0f;

        while (t < tumblingDuration)
        {
            t += Time.deltaTime;
            transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime);
            yield return null;
        }

        transform.rotation = endRotation;
        transform.position = endPosition;

        isTumbling = false;
    }
}
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – rutter Apr 16 '18 at 19:06

1 Answers1

0

The problem occurs because of precision problem with float variables. Google about float calculation problem or something similiar and you will get the point. Doing many multiplications and divisions on floats usually results in some accuracy flaws, as there's more and more aproximation in results.

Solution

To solve the problem you shoul round the endPosition. There's many ways to do that, for example:

endPosition = new Vector3((int)endPosition.x, (int) endPosition.y, (int) endPosition.z);

or

endPosition.x = (int)endPosition.x;
endPosition.y = (int)endPosition.y;
endPosition.z = (int)endPosition.z;
Community
  • 1
  • 1
tsvedas
  • 1,049
  • 7
  • 18
  • 1
    Note that the problem isn't specifically due to `float` precision, it's a result of limited precision in general. Any data type that has limited space to store values will have limited precision and will end up rounding values that don't fall exactly in line with it, causing a gradual build up of inaccuracy over the course of repeated calculations. – SilentSin Apr 17 '18 at 05:17