- Home /
Jarring "snap" on 2d Camera raycast collider
Hi, all.
I'm making an oldschool 3rd person racing game, with a camera mounted behind the player. The player is confined by walls on either side of the track, and if you get to close to said walls, the camera will clip out of the track.
My solution is to have the camera normally always be at a "camera mount" GameObject attached to the player. However, the mount raycasts out in either direction to detect the track walls. If it detects either wall, it will "push" the camera away by making its distance 10m from the wall.
So, basically, if the mount's raycast detects a wall, the math ought to be:
_playerCamera.transform.position = _mount.transform.position - transform.right * (10 - Vector3.Distance(hit.point, transform.position));
in other words, if the player is 6m from a wall: Camera Position = mount position + (10m - 6m) = mount position + 4m therefore, the camera should be offset by 4m.
However, I find there is a distracting "snap" effect when the mount detects a wall and begins of setting the camera, though I feel like if the math is sound, the transition between the "normal" state of the camera being at the mount's position and the "wall-detected" state of ofsetting the camera from the mount should be undetectable.
I also notice that when the player moves closer to the wall after the mount has detected a wall, the camera does not remain a uniform distance from the wall, which again, it should do if the math works.
Anyway, I feel like the solution is a fairly simple one, but it's eluding me at the moment. Thanks in advance for any help you folks can provide.
Your answer
Follow this Question
Related Questions
RTS camera help 1 Answer
Ray Cast not working for camera. 1 Answer
Place and size a cube at farplane of camera 1 Answer
How to use both NGUI widgets and unity UI 0 Answers
Distortion of mountains 1 Answer