- Home /
Velocity value changes way to drastically when position quickly changes
Hi
I've managed to recover some head bob code from one of the old player controllers by Unity that was released before 4.6 and I am running into some problems with the camera flying up in the air when the player position quickly changes. The position change is caused by the local position changing when I enter a moving platform. I simply set the player's parent to the moving platform and then set the parent back to what it was from the start. But when doing this the "springVelocity" float goes bonkers. I managed to pause the game when the camera freaked out and the spring velocity got set to -28 and it normally stays around the -1 area. It only happens for like 1 frame but it is very noticeable.
This is the code for the particular part:
Vector3 velocity = (plTransform.localPosition - prevPosition) / Time.deltaTime;
Vector3 velocityChange = velocity - prevVelocity;
prevPosition = plTransform.localPosition;
prevVelocity = velocity;
springVelocity -= velocityChange.y;
springVelocity -= springPos * springElastic;
springVelocity *= springDampen;
You might be thinking I should use the global position but that would make the head bob play when moving on a platform but the player is standing still. And if I added a bool to check if you are on a platform and then not play it wouldn't play at all while moving on the platform.
So any help with this is greatly appreciated!
Your answer
Follow this Question
Related Questions
Why camera.velocity is not consistant? 1 Answer
2D Enemy chasing player not at a constant speed,Enemy following player at constant speed 0 Answers
how to find forwards,right,up component of a velocity 2 Answers
How do I preserve float accuracy in transform.position? 1 Answer
Using Velocity to Switch Animations 1 Answer