public Vector2 deltaPosition = new Vector3(0, 1.5f);
public float MovementSpeed = 0.01f;
private Vector2 desiredPosition;
private Rigidbody2D rigidbody2D;
void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
desiredPosition = rigidbody2D.position;
}
// Call this function using your button
public void TriggerMovement()
{
desiredPosition = rigidbody2D.position + deltaPosition;
}
// Optional, just for easy debug
void Update()
{
if( Input.GetKeyDown(KeyCode.Space) )
TriggerMovement();
}
void FixedUpdate()
{
if( (rigidbody2D.position - desiredPosition).sqrMagnitude > MovementSpeed * 0.01f ) // Change threshold if needed
{
rigidbody2D.MovePosition(Vector3.MoveTowards(rigidbody2D.position, desiredPosition, MovementSpeed)) ;
}
}
ORIGINAL ANSWER
When creating games, having a scientific background is very useful.
In your situation, it seems you want your player to jump at a given height. Jumping is a little bit like throwing something in the air, so the rules of projectile motion apply here.
rb.MovePosition may not be the best way to go about this.
Try this; first make a variable called distance and set it to that 1.5f value you want.
then, wherever you are calling rb.MovePosition, do this instead:
transform.Translate(0, distance, 0);
Not tested, I might need to work on that, but if you try it and it does not work, let me know and I will figure it out for you when I have access to Unity.