#Preserving player's local y velocity correctly

1 messages · Page 1 of 1 (latest)

rapid surge
#

How would i go about getting and preserving the player's local y velocity, i am making a game where you can walk on a planet (player is a child of the planet), currently the movement works fine but when i walk to the side of the planet the physics start to act weird (eg. player starts to fall diagonally, player jumps diagonally from the pov of the player), i want to preserve the player's y velocity but somehow in local space? how do i do this?

movement method:

Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
Vector3 targetMoveAmount = moveDir * walkSpeed;
moveAmount = Vector3.Lerp(moveAmount, targetMoveAmount, Time.deltaTime * 8);

private void Movement()
{
     float currentYVelocity = rb.velocity.y;

      Vector3 newVelocity = orientation.TransformDirection(moveAmount);

      newVelocity.y = currentYVelocity;

      rb.velocity = newVelocity;

      Debug.DrawRay(transform.position, newVelocity);
}```
craggy elbow
#

did you delete this thread in code-general?

rapid surge
#

yes

craggy elbow
#

well, the answer i posted in there should still hold

rapid surge
craggy elbow
#

you'll need a vector pointing towards the center of the planet

#

so, pointing in the direection of gravity

rapid surge
#

yeah i have that, i can just use the rotation of the player since it's always rotated towards the gravity

craggy elbow
#

Vector3.Project(rb.velocity, gravityVector) will give you the part of the player's velocity that aligns with the gravity vector

rapid surge
#

something along these lines?

// get the gravity vector
Vector3 gravityVector = -transform.position.normalized;

// project the player's velocity onto the gravity vector
Vector3 gravityAlignedVelocity = Vector3.Project(rb.velocity, gravityVector);

// get the local movement
Vector3 localMoveAmount = orientation.TransformDirection(moveAmount);

// combine local movement with gravity-aligned velocity
Vector3 newVelocity = localMoveAmount + gravityAlignedVelocity;

// set the new velocity
rb.velocity = newVelocity;

// debug draw the resulting velocity
Debug.DrawRay(transform.position, newVelocity);