The question is answered, right answer was accepted
Why are my X, Y and Z position values going all wonkey?
Hey all, I'm a noob to Unity and game engines in general so I'm sorry if my question seems redundant or overly simple. When I play the scene I have so far the position values of the player object (just a cube for now) are all 0.00. I noticed the issue when I had the cube jump and land in pretty much the exact same position. Before the cube left that spot the posotion values would be (0, 0, 0) according to the Inspector, when it lands however the X and Z are still zero but the y seems to be a random value every time I jump and land. If I move the cube along the x or Z then all the position values go helter skelter. Any ideas?
Answer by FlaSh-G · Jan 06, 2018 at 08:19 PM
Float imprecision. Basically an effect that is caused by the fact that your X, Y and Z values are 32 bits of data each. These 32 bits are supposed to be able to represent very large numbers (1,000,000) and very small numbers (0,0000001). The way floating point numbers do this introduces imprecisions in calculation results. As long as your gameplay "feels fine", you shouldn't worry about these while developing a game.
Thank you FlaSh-g, although the inacuracy of the values are quite extreme, after each jump and landing the cube could be anywhere between -10.0 and 10.0 when it should be 0.00. This could be a problem if I sould ever want to have something happen depending on a positional value. is there no way of avoiding this?
Wait, 10m? That's not float inaccuracy. How does your jump work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed = 20;
public float jumpspeed = 1;
public float collected = 0;
public bool isDoorOpen = false;
public GameObject coin;
Rigidbody rb;
// Use this for initialization
void Start () {
print(collected);
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
if (Input.Get$$anonymous$$eyDown("space"))
{
Vector3 jump = new Vector3(0, 1, 0);
rb.velocity = jump * jumpspeed;
}
if (Input.Get$$anonymous$$eyDown("e"))
{
speed = Random.Range(1, 20);
print(speed);
}
if (collected == 12)
{
isDoorOpen = true;
collected = 0;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
collected += 1;
print(collected);
}
}
}
It's messy and disfunctional, I know. my appologies, today is my forst time using unity without instruction so I'm just kinda throwing code about seeing what I can make it do lol.
So... you have a flat and even ground. You put this script on a cube and hit space. The thing flies in the air and lands 10m off the starting point? Without you pressing any other key to move into a certain direction?
That sounds really hard to believe.
Is it possible that the inspector shows a really small number (x.xxxxxxxE-xx)? Or that there is something interfering? Try stripping your scene from everything not directly related and your code from everything but the jump part.
thanks again FlaSh-G, the cube doesn't move. if I have it jump it will land in exactly the same place visually but the values in the inspector will say it's still in the air or under the floor. Jumping only has this effect on the inspector's Y position. if I move on the X or Z position the Inspector seems to be under the impression that the cube has moved on all axis to some random extent.
Also I have updated from 2017 2 of 3 - 2017 3 of 3 and reinstalled, made no change.
Reinstalling Unity has never solved any project-related issues :)
Concerning your problem... I have no idea why it would display plus or $$anonymous$$us 10m off the actual position. Have you checked whether the number is actually really small (with that exponent part)?
yes, it is the format that you mentioned earlier, 2.86326e-09. me being a dumb*** I didn't realise the number went on past the visible digits. what is going on with this number, I'm not entirely sure what's going on with it.
I've just done a bit of research. I'm so sorry To have wasted your time and thank you for your patience.
yes, it is the format that you mentioned earlier, 2.86326e-09. me being a dumbass I didn't realise the number went on past the visible didgits. what is going on with this number, I'm not entirely sure what's going on with it.