When a player hops on a rail in my game I set friction to 0 and make the railGrindingSFX volume to 1.
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if ( hit.transform.CompareTag("Rail") )
{
velocity = new Vector3(cam.forward.x, 0, cam.forward.z) * railSpeed;
Accelerate(acceleration);
speed = Mathf.Clamp(speed, baseSpeed, maxSpeed);
friction = 0;
railGrindingSFX.volume = 1;
}
}
but since there’s no OnControllerColliderExit I have to constantly update these values to be their defaults in updates and I need to do it right before the OnControllerColliderHit runs so that it can override the default values.
Luckily, it says that OnControllerColliderHit is called when performing a move operation, which should mean that this should work right?
railGrindingSFX.volume = 0;
friction = baseFriction;
mover.Move(direction * speed * Time.deltaTime);
But this doesn’t work. The friction stays at the default value while I’m on the rail. If I change it to this instead:
private void OnControllerColliderHit(ControllerColliderHit hit)
{
friction = baseFriction;
railGrindingSFX.volume = 0;
if ( hit.transform.CompareTag("Rail") )
{
velocity = new Vector3(cam.forward.x, 0, cam.forward.z) * railSpeed;
Accelerate(acceleration);
speed = Mathf.Clamp(speed, baseSpeed, maxSpeed);
friction = 0;
railGrindingSFX.volume = 1;
}
}
this will work except when I get off a rail, it won’t reset the values until I land on something that isn’t a rail. Is this a bug in the character controller or something?