camera wont stop stuttering
hi i am having problems with camera script where i added raycast to give it collision so it will get closer the the player when it comes into contact with terrain and now it wont stop stuttering but the camera collides with the wall and goes to the player perfect but the camera shaking is annoying, heres my code could someone tell me where i went and how i can fix it
using UnityEngine;
public class CameraMovement : MonoBehaviour {
public Transform player_cam, center_point;
public float distance, max_height, min_height, orbiting_speed, vertical_speed;
float height;
Vector3 dest;
RaycastHit hit;
void Update () {
center_point.position = gameObject.transform.position + new Vector3 (0, 1.57f, 0);
center_point.eulerAngles += new Vector3 (0, Input.GetAxis ("Mouse X") * Time.deltaTime * orbiting_speed, 0);
height += Input.GetAxis ("Mouse Y") * Time.deltaTime * -vertical_speed;
height = Mathf.Clamp (height, min_height, max_height);
}
void FixedUpdate () {
dest = center_point.position + center_point.forward * -1 * distance + Vector3.up * height;
if (Physics.Linecast (center_point.position, dest, out hit)) {
if (hit.collider.CompareTag ("Terrain")) {
player_cam.position = hit.point + hit.normal * 0.3f;
}
}
player_cam.position = Vector3.Lerp (player_cam.position, dest, Time.deltaTime * 10);
player_cam.LookAt (center_point);
}
}
Comment
Don't set the position in FixedUpdate. use LateUpdate/Update ins$$anonymous$$d
And this should fix it if i put the player cam. Position in lateupdaye or update?
I guess something like this:
void LateUpdate()
{
dest = center_point.position + center_point.forward * -distance + Vector3.up * height;
Vector3 v = dest - center_point.position;
float magnitude = v.magnitude;
v /= magnitude;
Ray ray = new Ray(center_point.position, v);
if (Physics.Raycast(ray, out hit, magnitude))
{
if (hit.collider.CompareTag("Terrain"))
{
dest = hit.point + v * 0.3f;
}
}
player_cam.position = Vector3.Lerp(player_cam.position, dest, Time.deltaTime * 10);
player_cam.LookAt(center_point);
}