# Call a function the first time a ray stops detecting a collision?

So I may be missing something here, but I am currently working on wall running in my game. Basically, I use Quaternion.Slerp to tilt the camera left or right to make it look nice. That works fine. But there is a problem. I (obviously) have a movement script, and in there I control looking around with the mouse. My wall running script is separate, but basically what is happening is that that my wall running script is constantly ‘Slerping’ the camera, which causes a small, but annoying jitter in the player’s camera. So what I want to try to do is only call the rotate function the first time the ray stops detecting that wall, and I think that would help. If you know any better ways of doing this, please let me know too.

Edit: My camera rotating code in the wallrun class

void RotateCamera(int side) // 0 for right, 1 for left
{
// Rotate camera to the right
if (side == 0)
{
playerCam.localRotation = Quaternion.Slerp(playerCam.localRotation, Quaternion.Euler(playerCam.localRotation.x, 0, cameraRotateAmount), Time.deltaTime * camRotateSpeed);
}
// Rotate camera to the left
else if (side == 1)
{
playerCam.localRotation = Quaternion.Slerp(playerCam.localRotation, Quaternion.Euler(playerCam.localRotation.x, 0, -cameraRotateAmount), Time.deltaTime * camRotateSpeed);
}

// Will rotate camera back to (x, 0, 0)
else if (side == 2)
{
playerCam.localRotation = Quaternion.Slerp(playerCam.localRotation, Quaternion.Euler(playerCam.localRotation.x, 0, 0), Time.deltaTime * camRotateSpeed);
}
}

Physics.Raycast returns false when it hits nothing. You could have a bool for tracking what the result of the previous raycast was, and if the current raycast returns false and the previous one returned true, you do your rotate.

I have no idea if this will help with your jitter issue though. It is usually best to mess with the camera position and rotation in LateUpdate so everything else in the scene has done its repositioning before the camera moves. No idea if you are already doing that.

Alrighty, I will have a play around with that stuff. Cheers!

Edit: Wow, just putting the movement’s camera look function into LateUpdate fixed this. Many thanks!

1 Like