- Home /
Trying to fix bouncy linecast camera collision
Currently I'm scaling a value from 1 to 0 that my camera distance is multiplied by. If the linecast hits the ground layer behind my camera, it scales the value down so the camera goes closer to my player, otherwise if the linecast isn't hitting anything the value goes up.
Right now whenever my linecast is hitting the ground layer, the camera gets closer to my player, but then the two values fight each other so it's really jittery.
if (Physics.Linecast(target.position, cameraOffset.position, mask) && camCollisionValue >= 0.2f)
{
camCollisionValue = camCollisionValue - .02f;
}
else if (!Physics.Linecast(target.position, cameraOffset.position, mask) && camCollisionValue < 1.0f)
{
camCollisionValue = camCollisionValue + .02f;
}
It looks like you have to refine you multiplier and the size of the linecast.
I have suggestion for you, create a variable out of the result of linecast, just like this:
var hit = Physics.Linecast(target.position, cameraOffset.position, mask)
It seems to me that the size of the linecast isnt enough to make this stop bouncing.
Have you tried using raycast?
Answer by KenjiJU · Oct 06, 2017 at 03:08 AM
So I got some really nice help help fixing this issue on discord. Raycast instead of Linecast. I already had camera movement set up a certain way, so the raycast was placed as the very end of the movement.
Vector3 towardCamera = (transform.position - target.position).normalized;
Ray ray = new Ray(target.position, towardCamera);
RaycastHit info;
if (Physics.Raycast(target.position, towardCamera, out info, dstFromTarget, mask))
{
transform.position = info.point;
}
Your answer
Follow this Question
Related Questions
Can someone modify this for me? 0 Answers
Top down camera lock 2 Answers
Raycast doesn't collide as it should 1 Answer
Weird error with RayCasting (CS1502) 1 Answer
How can I change this script? 0 Answers