Hi. I’m trying to make a character movement script which allows the player to move freely on any surface. My problem is that the player doesn’t accelerate nor decelerate when the player goes vertically on a wall. Any idea what’s the problem? Here’s my code:
private void Move()
{
Vector3 targetSpeed = movement * speed;
Vector3 speedDif;
speedDif = targetSpeed - Vector3.ProjectOnPlane(Vector3.Scale(Vector3.ProjectOnPlane(new Vector3(1, 0, 1), groundNormal), rb.velocity), Vector3.up);
float accelRateX;
float accelRateZ;
if (groundCheck.Length > 0)
{
accelRateX = (Mathf.Abs(targetSpeed.x) > 0.01f) ? acceleration : deceleration;
accelRateZ = (Mathf.Abs(targetSpeed.z) > 0.01f) ? acceleration : deceleration;
}
else
{
accelRateX = (Mathf.Abs(targetSpeed.x) > 0.01f) ? acceleration * airAccelMultiplier : deceleration * airDecelMultiplier;
accelRateZ = (Mathf.Abs(targetSpeed.z) > 0.01f) ? acceleration * airAccelMultiplier : deceleration * airDecelMultiplier;
}
Vector3 finalVel = Vector3.Scale(speedDif, new Vector3(accelRateX, 0, accelRateZ));
rb.AddForce(Vector3.ProjectOnPlane(finalVel, groundNormal), ForceMode.Force);
Debug.Log($"finalVel: {finalVel}");
Debug.Log(Vector3.ProjectOnPlane(movement, groundNormal));
}
It’s a 3d version of this guy’s 2d platformer controller https://www.youtube.com/watch?v=KbtcEVCM7bw
Also, my character has a “no friction” physics material on and it’s gravity is disabled when climbing slopes.