How to get transform position without delay?

I am trying to memorize a specific position. But I always get a left or right offset and never the exact position.

I think it may be related to the frame rate but I’m not sure about it.

This is my code, and i am executing it inside of FixedUpdate().

    private bool SetPositionToJumpOnPlatform()
    {

        float col_x = (boxCollider.size.x) + boxCollider.offset.x;  
        float jumpDistance = Mathf.Abs(Mathf.Cos(sensor.roof.angle) * sensor.roof.distance);      
   
        if (freeWay.up && !freeWay.positionAnnotated)
        {
            freeWay.position = new Vector2(transform.position.x,0f);
            freeWay.readyToJump = false;
            freeWay.positionAnnotated = true;
        }

        if (!freeWay.positionAnnotated)
        {  
            return false;
        }

     
        if ( direction == Vector2.right && !freeWay.readyToJump)
        {
            Debug.Log(transform.position.x + " " + freeWay.position + " " + (freeWay.position.x + jumpDistance + col_x));

            if (transform.position.x<= freeWay.position.x + jumpDistance + col_x)            {
                moveTo.right = true;
            }
            else
            {
                freeWay.readyToJump = true;
                freeWay.positionAnnotated = false;
                transform.FlipToLeft();
                return true;
            }
        }

 

        if ( direction == Vector2.left && !freeWay.readyToJump)
        {
            Debug.Log(transform.position.x + " " + freeWay.position + " " + (freeWay.position.x - jumpDistance - col_x));

            if (transform.position.x >= freeWay.position.x - jumpDistance - col_x)
            {  
                moveTo.left = true;
            }
            else
            {
                freeWay.readyToJump = true;
                freeWay.positionAnnotated = false;
                transform.FlipToRigth();
                return true;
            }
        }
        return false;
    }

This is my console output (see attached image):

Does anyone know how to get the exact position correctly? if it is possible …

Thank you so much

It doesnt appear you are using any physics so you should move it to the Update function. That should probably get you a better result. Only use FixedUpdate when you are dealing with physics.

Hello, thank you for your comment. Yes, I tried within Update () as well, but it kept giving the same problem.

I was using FixedUpdate () because it runs more times than Update () in the same time.

I thought that this way I could gain a bit of precision, but no, it doesn’t work either :frowning:

So continue to use Update for that function, you can read the docs to see that FixedUpdate should only be used for physics.

As for your issue, in your console (which is very small and hard to read), it only shows the 2 floats but in your Debug.Logs in your code, you are adding values together which is why you are getting differences. Try just using Debug.Log(transform.position.x, transform.position.y);

I dont know what the freeWay object is but if you need it as well, do the same thing with the debug.log as i mentioned above. Once you have a simple X, Y coords for the gameobject and your freeWay object, you may be able to add/subtract/whatever them together in a better way.

Ok, i will, thank you for your time.