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?
2 Answers
2Float 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?
– ActiveMars91So... 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.
– FlaSh-GReinstalling Unity has never solved any project-related issues :) Concerning your problem... I have no idea why it would display plus or minus 10m off the actual position. Have you checked whether the number is actually really small (with that exponent part)?
– FlaSh-Gusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
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.GetKeyDown("space"))
{
Vector3 jump = new Vector3(0, 1, 0);
rb.velocity = jump * jumpspeed;
}
if (Input.GetKeyDown("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);
}
}
}
just delete the csproj files in your projects folder. They get recreated when your ide starts. Did you try this: [OLD POST][1]? [1]: https://answers.unity.com/questions/574256/monodevelop-401-wont-recognize-unityengine-namespa.html
– Captain_Pineapple