- Home /
Need help with third-person camera stuttering involving raycasts and clamp
I'm trying to create a script for a third-person camera that will prevent the camera from having its view obstructed by walls by having the camera move in front of the player. The script works but the camera stutters very badly and I've tried rearranging functions inside Update/FixedUpdate for the last couple of days to no avail.
**Edit: I found one possible source to the problem and that is Raycast simultaneously returns true and false. I tested Raycast in a brand new project with very basic code and Raycast does the exact same thing when it's put into any Update/OnTriggerStay. At this point, I have no idea on why it's doing this. How can I get Raycast to only return true or false exclusively?
public float mouseSens; //mouse sensitivity
public float dstFromTarget;
public Transform target; //the target the camera follows
public Vector2 pitchMinMax; //pitch range values in the form of a vector2
public Vector2 scrollMinMax;
public Light minimapLight;
public float rotationSmoothTime;
Vector3 rotationSmoothVel;
Vector3 currentRotation;
public RaycastHit collisionInBetween;
bool collisionExists;
float yaw; //horizontal camera movement
float pitch; //vertical camera movement
float scrollFactor;
float startClipPlaneValue;
void Start () {
scrollFactor = 0.2f;
scrollMinMax = new Vector2(20, 30);
dstFromTarget = scrollMinMax.y/scrollMinMax.x;
mouseSens = 3.5f;
pitchMinMax = new Vector2(-60, 85);
rotationSmoothTime = 0.12f;
startClipPlaneValue = 2.0f;
}
void Update () {
yaw += Input.GetAxis("Mouse X") * mouseSens; //moves camera horizontally using mouse leftright
pitch -= Input.GetAxis("Mouse Y") * mouseSens; //moves camera vertically using mouse updown
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y); //set the pitch range limit
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVel, rotationSmoothTime);
transform.eulerAngles = currentRotation;
ScrollForDistance();
if (collisionExists)
{
//Debug.Log(targetToCamera.magnitude + ", " + (collisionInBetween.point - target.position).magnitude);
//Camera.main.nearClipPlane = (collisionInBetween.point - target.position).magnitude;
//Debug.DrawLine(transform.position, collisionInBetween.point, Color.red);
float rayToTargetDistance = (collisionInBetween.point - target.position).magnitude;
if (rayToTargetDistance <= (transform.forward * dstFromTarget).magnitude)
transform.position = target.position - transform.forward * rayToTargetDistance;
}
else
{
transform.position = target.position - transform.forward * dstFromTarget; //make the position of this camera the same as the target's position
}
}
private void FixedUpdate()
{
dstFromTarget = Mathf.Clamp(dstFromTarget, scrollMinMax.x, scrollMinMax.y);
Vector3 targetToCamera = transform.position - target.position;
Vector3 offsetValue = transform.forward * Camera.main.nearClipPlane; //from camera's forward transform vector, offset forward by cameranearplane units.
Vector3 targetToCameraOffset = targetToCamera + offsetValue; //add offset starting from transform.forward and facing forward with respect to this object
collisionExists = Physics.Raycast(target.position, targetToCamera, out collisionInBetween, targetToCameraOffset.magnitude);
}
private void OnPreCull()
{
minimapLight.enabled = false;
}
private void OnPostRender()
{
minimapLight.enabled = true;
}
void ScrollForDistance() {
if (Input.GetAxis("Mouse ScrollWheel") > 0f) // forward
{
dstFromTarget -= scrollFactor;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f) // backwards
{
dstFromTarget += scrollFactor;
}
}
Your answer
Follow this Question
Related Questions
Raycasting with Rotation of Camera 0 Answers
Get 2D Collider with 3D Ray 2 Answers
Move character in direction its facing 1 Answer
Start raycast from object 1 Answer