- Home /
Preventing change in X position when jumping
Hi, i'm making a 2d game demo.My player has rigidbody component.I freezed all of rotations and z position.My problem is if i freeze x position, it causes collision errors on edge of platforms or corner of platforms and player is jittering.On the otherhand if i don't freeze x position , after the jump when player is on ground, x position of player slightly changing and player looks weird, like shaking, player a little bit getting smaller then getting normal scale .
My question is how can i prevent changing in x position without using freeze position?
Thank You ...
Answer by YoungDeveloper · Aug 12, 2013 at 05:48 PM
You could save the position each frame in a variable. And in update function make a check, if player is jumping players position x = saved position x.
thank you for your answer, i saved position and load after jump but it's not work properly.When the player touchs ground, first its position change slightly then move back saved position , so still there is change in x position. Here is my code may be i should add some staff or change it
if (onGround == false) { PlayerPrefs.SetFloat("xPos", transform.position.x); } if(amountOf$$anonymous$$oveX == 0f) { float xPosition = PlayerPrefs.GetFloat("xPos", transform.position.x); rigidbody.transform.position = new Vector3(xPosition, transform.position.y, transform.position.z); }
Your answer
Follow this Question
Related Questions
Problem with joints 2 Answers
Drag and jump mechanics? 2 Answers
My player wont jump (2d game) 1 Answer
How Do I Create Random Platforms in a 2d Game? 0 Answers
I Can't Jump, And If I Remove if(isGrounded) I Can Do More Then 10 Jumps IN The Air 1 Answer