- Home /
bounce player back to position while jumping
ive got a player that bounces off an object, my map is a tile structure and when the player bounces off an object he gets pushed back 2 tiles this works fine except when i'm jumping my jump function is an enumerator that lerps the player if he starts at 0,0,1 then it will lerp to 0,1,2 then lerps the player again to 0,0,3 so it travels up and down over two tiles my floor is tagged to let me know when he is on it, if i jump from one tile in front of a bouncer the whole thing works perfectly i bounce off the object and the player is put on the ground as expected however if i jump from 2 tiles in front of the bouncer i land on the bouncer and this pushes my player back the 2 tiles but up higher somewhere around 0,2,0 (assuming the bouncer is at 0,0,2) I'm not using any force or velocity just transform can anyone help?
first here is my jump coroutine
IEnumerator Jump()
{
isMoving = true;
isGrounded = false;
StartPosition = transform.position;
if (Facing == 0)
{
EndPosition = transform.position - (new Vector3(0.0f, -2.0f, GridSize));
}
if (Facing == 1)
{
EndPosition = transform.position - (new Vector3(GridSize, -2.0f, 0.0f));
}
if (Facing == 2)
{
EndPosition = transform.position - (new Vector3(0.0f, -2.0f, -GridSize));
}
if (Facing == 3)
{
EndPosition = transform.position - (new Vector3(-GridSize , -2.0f, 0.0f));
}
t = 0.0f;
jumpSource.PlayOneShot(jumpNoise, 1f);
while (t < 1.0)
{
t += Time.deltaTime * (WalkSpeed / GridSize);
transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
yield return new WaitForSeconds(0);
}
if (!isGrounded)
{
StartPosition = transform.position;
if (Facing == 0)
{
EndPosition = transform.position - (new Vector3(0.0f, 2.0f, GridSize));
}
if (Facing == 1)
{
EndPosition = transform.position - (new Vector3(GridSize, 2.0f, 0.0f));
}
if (Facing == 2)
{
EndPosition = transform.position - (new Vector3(0.0f, 2.0f, -GridSize));
}
if (Facing == 3)
{
EndPosition = transform.position - (new Vector3(-GridSize, 2.0f, 0.0f));
}
t = 0.0f;
while (t < 1.0)
{
t += Time.deltaTime * (WalkSpeed / GridSize);
transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
yield return new WaitForSeconds(0);
}
}
isMoving = false;
turnRight = false;
turnLeft = false;
jumping = false;
//isGrounded is true on contact with the floor
}
here is my goBackwards coroutine it is called when i bounce
IEnumerator GoBackward()
{
isMoving = true;
if (Facing == 0)
{
EndPosition = StartPosition - (new Vector3(0.0f, 0.0f, -GridSize));
}
if (Facing == 1)
{
EndPosition = StartPosition - (new Vector3(-GridSize, 0.0f, 0.0f));
}
if (Facing == 2)
{
EndPosition = StartPosition - (new Vector3(0.0f, 0.0f, GridSize));
}
if (Facing == 3)
{
EndPosition = StartPosition - (new Vector3(GridSize, 0.0f, 0.0f));
}
t = 0.0f;
while (t < 1.0)
{
t += Time.deltaTime * (WalkSpeed / GridSize);
transform.position = Vector3.Lerp(transform.position, EndPosition, t);
yield return new WaitForSeconds(0);
}
isMoving = false;
}
i think i need to check inside the goBackwards coroutine to see the player was jumping or !isGrounded but it could be deeper with me using coroutines any help on this would be greatly appreciated
Your answer
Follow this Question
Related Questions
Static Coroutine being called endlessly 2 Answers
Why is this coroutine only firing once? 3 Answers
Weapon Bullet Question - Steadily increasing sine wave 1 Answer
smooth fps movements using joystick 1 Answer
Script Crashing 1 Answer