I'm trying to make it so that when the player in my game moves left or right, he lightly jumps from his current position to his new position. The way I have it set up now, the jump occurs once he has arrived at his new position. What can I do to make it so that the jump happens sooner so that it appears that he jumps to his new position? Here is the code I have that controls that part of his movement:
if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x > minXPos)
{
targetPos = new Vector3(transform.position.x - xIncrement, transform.position.y, transform.position.z);
rb.AddForce(Vector3.up * jumpForceTwo, ForceMode.Impulse);
transform.position = Vector3.MoveTowards(targetPos, transform.position, speed);
isHorizontalingLeft = true;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < maxXPos)
{
targetPos = new Vector3(transform.position.x + xIncrement, transform.position.y, transform.position.z);
rb.AddForce(Vector3.up * jumpForceTwo, ForceMode.Impulse);
transform.position = Vector3.MoveTowards(targetPos, transform.position, speed);
isHorizontalingRight = true;
}
Here is a video showing what is happening (sorry it's blurry): https://youtu.be/rPewtb8QywE
Time.deltaTime
and the speed is too high. It would probably be a nightmare to handle horizontal movement with forces in this type of game, thoughRigidBody.MovePosition()
is still better than transform.position \$\endgroup\$