I am currently making a game the encorporates the bhop mechanic from the source engine. I am using a method to return a vector that I will move my player with Using the transform.Translate() function it works well, and transforms the player in the local space, but passes straight through objects with colliders. Using the characterController.Move() function it collides with objects but transforms in the global space. To get around this I use transform.TransformVector() and it does not move it the way it should move.
Vector3 moveDir = new Vector3(0, 0, 0);
moveDir = MoveGround(this.accelDir, this.prevVelocity) * Time.fixedDeltaTime;
moveDir = transform.InverseTransformVector(moveDir);
characterController.Move(moveDir);
if(Input.GetKeyDown(KeyCode.Space))
{ Jump(); } }
//This calculates and returns the players velocity based on the current velocity and the desired acceleration direction
Vector3 Accelerate(Vector3 accelDir, Vector3 prevVelocity, float accelerate, float max_velocity) {
float projVel = Vector3.Dot(prevVelocity, accelDir); // Vector projection of current velocity onto accelDir
float accelVel = accelerate * Time.fixedDeltaTime;
// Truncate the acelerated velocity so that the projection does not exceed the max velocity
if ((projVel + accelVel) > max_velocity) {
accelVel = max_velocity - projVel;
}
return prevVelocity + accelDir * accelVel;
}
//This scales then returns the velocity based on friction
Vector3 MoveGround(Vector3 accelDir, Vector3 prevVelocity) {
float speed = prevVelocity.magnitude;
if (speed != 0) {
float drop = speed * friction * Time.fixedDeltaTime;
prevVelocity *= Mathf.Max(speed - drop, 0) / speed;
}
return (Accelerate(accelDir, prevVelocity, ground_accelerate, max_velocity_ground));
}