Search Unity

Kinematic object moving by itself when game runs

Discussion in 'Physics' started by mrm83, Mar 16, 2018.

  1. mrm83

    mrm83

    Joined:
    Nov 29, 2014
    Posts:
    345
    I am stumped and I don't know what is going on.

    I have a gameobject with rigidbody2d attached, it is set to kinematic so it SHOULD NOT move.

    X is at 12, and when the game runs, X becomes 12.00005

    There is nothing else attached, only the rigid body.

    Why is my object moving? Bug?
    It appears that the position can not be set too.
    If i set it to 13, it becomes 13.0005.
    If i set it to 13.55, it becomes 13.54998

    Update:
    It appears this is caused by "Simulate", not sure what this is as I recently upgraded from 5.3 to 5.6. Why is Simulate altering the position of a kinematic/static object? What is simulate?
     
    Last edited: Mar 16, 2018
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    How are you checking the transform of these objects? Are you using debug.log()? Also, is 12.00005 really not good enough? 12.00005 is essentially 12.

    Edit: also are you setting it to isKinematic in script or the inspector?
     
  3. mrm83

    mrm83

    Joined:
    Nov 29, 2014
    Posts:
    345
    I am inspecting it from editor.

    I am using the position to check for some other stuff so it needs to be an exact match. Kinematic is set from editor.

    No, 12.00005 is not 12. 12 is 12, 12.00005 is 12.00005.

    The fact is that static objects are meant to be static and it should not move. Moving by .00005 is moving, thus not static.

    Update:
    I tried to disable simulate, position is fixed but trigger no longer works.
     
  4. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    I'm not sure why its different you may have to post some screenshots.

    I asssume you are trying to compare float/double values like this:
    Code (CSharp):
    1. if(float1==float2)
    2. {
    3. doSomething();
    4. }
    If so it is a bad idea since float values will rarely equat on that level of accuracy. You should compare aproximate values instead or whole numbers.

    An easy solution would be simply to set the position via script at runtime. Then it will be 12.000000000000000000000000 so that away you can have the accuracy on a subatomic scale.
     
  5. mrm83

    mrm83

    Joined:
    Nov 29, 2014
    Posts:
    345
    I care about the vector position and that is what I am comparing.

    Vector2 a = Vector2(12,5);
    Vector2 b = Vector2(12.00005, 5);

    is a == b ? false

    As i've mentioned previously, setting the position does not set the position I want.
    If i set vector x to 13, it becomes 13.0005.
    If i set vector x to 13.55, it becomes 13.54998
     
  6. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207

    Vectors are just a set of float numbers. As I said before trying to compare two floats as you are doing is a bad idea but it's your project so do what you want.

    You never mentioned if you tried setting the position at runtime. Have you tried setting the position at runtime via script not inspector? That's what a meant in my last post. You never mention trying it via script.
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    You'll just need to accept that floating points numbers aren't perfectly accurate. I recommend implementing an IsCloseTo extension method, like this:

    Code (CSharp):
    1. public static bool IsCloseTo(this Vector3 self, Vector3 other, float epsilon) {
    2.     return Mathf.Abs(self.x - other.x) < epsilon && Mathf.Abs(self.y - other.y) < epsilon && Mathf.Abs(self.z - other.z) < epsilon;
    3. }
    Then, instead of `v1 == v2` in your code, you'd write `v1.IsCloseTo(v2, epsilon)`, where epsilon is some small float value, like 0.0001f. That's the typical way floats are compared.
     
    StickyHoneybuns likes this.