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