- Home /
Calculate player velocity change while taking into account velocity of what the player is standing on
I have a Rigidbody character who is controlled by adding force as a velocity change. I already have it set up so that external forces can still be applied (if I wanted to add an explosion force near the character, the character would respond naturally).
However, I decided to try to add a moving platform to see how it responded. I realized this did not work well with the current system. My platforms have a kinematic Rigidbody. I am moving them using Rigidbody.MovePosition with interpolation. This works fine for normal Rigidbody objects (a cube with Rigidbody and Box Collider stays on the platform just fine). The reason it is not working for the character seems to be due to setting the velocity as my means for moving the player. So, what I am trying to wrap my head around is a way to compensate for the ground’s velocity before setting my character’s velocity.
This is really more of a "because I can" thing. I would normally just parent the character to the platform, and call it a day. But I am interested in making this work...
This movement code is on the character and called every FixedUpdate:
//Calculate force to add:
float velocity;
if(crouching) { velocity = crouchVelocity; }
else if(running) { velocity = runVelocity; }
else { velocity = walkVelocity; }
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
Vector3 targetVelocity = (forward * vertical + right * horizontal) * velocity; //forward and right are custom Vector3s that mirror the right and forward of my camera
Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
Vector3 velocityChange = transform.InverseTransformDirection(targetVelocity) - localVelocity;
velocityChange = Vector3.ClampMagnitude(velocityChange, !jumping ? groundControl : airControl);
velocityChange.y = !jumping ? 0.0f : -localVelocity.y + jumpVelocity;
velocityChange = transform.TransformDirection(velocityChange);
//Apply force:
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
What I want to add is a way to calculate my velocityChange while taking into account the velocity of the ground I am standing on. That way I can still move and jump.
I naively tried adding this right before my AddForce:
GameObject ground = GetGround(); //Get ground checks for and returns ground if it is there
if(ground)
{
Rigidbody groundRigidbody = ground.GetComponent<Rigidbody>();
if(groundRigidbody)
{
velocityChange += groundRigidbody.velocity;
}
}
But of course, that resulted in my character accelerating off the platform since the next FixedUpdate the player would be adding in the velocity of the platform again even though he is already moving that fast.
I also tried to get the difference between the platforms velocity and the characters velocity and add that into velocity change, but that didn't seem to have the desired effect either. The character kind of kept up with the platform, but lagged behind and eventually fell off.
If I could get this working, it would mean my character could stand and move on any moving Rigidbody object. I just think that would be neat, albeit unrealistic in some (okay many) cases.
Your answer
![](https://koobas.hobune.stream/wayback/20220612164327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers
Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers
Inconsistent Rigidbody2D velocity. 0 Answers
What is the velocity on a given axis of a rigidbody? 2 Answers
GameObject disappears when velocity is set to (near) zero. 1 Answer