I am having an issue with the moveTowards rigidbody2D method. The way my game works is that the player shoots out a projectile at where the mouse is and it stops there. Then the player can dash in a straight line to reach the projectile, at which point it despawns. This works perfect when the player is on the ground and dashes to the object but if the player is in the air and dashes to the object, it always curves instead of being straight. I’m not sure what the cause of this issue is.
this is in my update function. It sets the bool playerDashing to true if the disc object is closer to its destination, which is called currentMouse, then the player is to the disc
if (Input.GetKeyDown(KeyCode.F) && discDeployed == true)
{
float dist = Vector3.Distance(currentMouse, currentDisc.transform.position);
float dist2 = Vector3.Distance(player.transform.position, currentDisc.transform.position);
if (discReturning == false && dist <= dist2)
{
playerDashing = true;
}
}
this is in my fixedUpdate function and it causes the player to move towards the disc when player dashing equals true
public void FixedUpdate()
{
//while this bool is true the player goes to disc
if(playerDashing == true)
{
player.GetComponent<Rigidbody2D>().gravityScale = 0;
player.transform.position = Vector3.MoveTowards(player.transform.position, currentDisc.transform.position, 20 * Time.deltaTime);
}
}
when the player is close enough to the disc object it is despawned and gravity goes back to normal. I know for a fact that that is not the issue as i checked it several times so i am omitting it unless you think it would be helpful