Home > OS >  facing down hill faux gravity
facing down hill faux gravity

Time:08-28

I have a FPS Faux gravity player controller it can do the basics Walk Run Jump Crouch but I'm now implementing Slide functionality to it and it works I have this Equation that calculates weather or not we should be decreasing in speed or increasing

moveSpeed = moveSpeed * ((slopeAngle / 2250) .99);

it works, it works well but we have a issue that the equation doesn't care if we are gong Uphill or down so, I can actually slide accelerate up a hill so how can I implement a system that will lower my speed if my direction is facing up hill and increase it when I'm facing down hill

I'm already using terrain normals so I need a way to figure out what the up direction of a terrain normal is and then plug it into my equation so when I'm facing down I accelerate and facing up the terrain normal I decelerate

CodePudding user response:

You could raycast forward in a forward direction from your feet to check if they are going uphill. If it hits something, then we will not calculate the sliding.

The height value must be slightly less than half your player's height Also, dist must be a low number, but not too low.

public float height = 0.99f; //--> Must be a little less than half the height
public float dist = 0.02f; //--> Must be a low number, but not too low


void Update()
{
  Ray ray = new Ray(transform.position - height, transform.forward);
  if (!Physics.Raycast(ray, dist))
  {
     moveSpeed = moveSpeed * ((slopeAngle / 2250)   .99);
  }
}

CodePudding user response:

You need to use the geometry of the situation:

You have two known vectors: n, the normal to the surface you are currently on, and g, gravity (both direction and magnitude).

You also have one desired vector: a, your acceleration due to sliding (this would then be added to any other accelerations)

Neglecting friction (which you probably don't want to do, but that's separate), your acceleration is given by the projection of the gravity vector onto the surface plane:

a = g - n * Vector3.Dot (g, n) / Vector3.Dot (n, n)

If n is known to be a unit vector, you can skip the division as an optimization.

This will take care of acceleration direction regardless of the slope and your current velocity, or even the direction of gravity. It will also work equally well in 2D or 3D (or any other dimension, really).

  • Related