There are two variables i’m using for forcing: PositionFolowForce and RotationFolowForce
I found out that even if i change both values to 1 the object in this case helicopter is shaking when i control it.
If i set both variables values to 0 then the helicopter will not shake but then camera will not follow it.
How can i fix it ? I want to use the force on both position and rotation but it’s making the helicopter shaking/stuttering.
using UnityEngine;
public class FollowTargetCamera : MonoBehaviour
{
public Transform Target;
public float PositionFolowForce = 5f;
public float RotationFolowForce = 5f;
public Vector3[] directions = new Vector3[] { Vector3.forward, Vector3.back
, Vector3.left, Vector3.right };
private int index = 0;
private Vector3 vector = new Vector3(0, 0, 0);
private Vector3 dir = new Vector3(0, 0, 0);
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
if (++index == directions.Length)
index = 0;
vector = directions[index];
dir = Target.rotation * directions[index];
}
dir.y = 0f;
if (dir.magnitude > 0f) vector = dir / dir.magnitude;
transform.position = Vector3.Lerp(transform.position, Target.position, PositionFolowForce * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vector), RotationFolowForce * Time.deltaTime);
}
}
Add rigidbody to the camera and move it with that instead of modifying the transform.
The problem is Physics updates on FixedUpdate and you are moving the camera in Update. The don’t work at the same frequency, sometimes FixedUpdate will happen multiple times in one frame.
It’s working this way. The part with the V key changing the view is in Update and the part of updating the position and rotation is in the FixedUpdate:
using UnityEngine;
public class FollowTargetCamera : MonoBehaviour
{
public Transform Target;
public float PositionFolowForce = 5f;
public float RotationFolowForce = 5f;
public Vector3[] directions = new Vector3[] { Vector3.forward, Vector3.back
, Vector3.left, Vector3.right };
private int index = 0;
private Vector3 vector = new Vector3(0, 0, 0);
private Vector3 dir = new Vector3(0, 0, 0);
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
if (++index == directions.Length)
index = 0;
vector = directions[index];
dir = Target.rotation * directions[index];
}
}
private void FixedUpdate()
{
dir.y = 0f;
if (dir.magnitude > 0f) vector = dir / dir.magnitude;
transform.position = Vector3.Lerp(transform.position, Target.position, PositionFolowForce * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vector), RotationFolowForce * Time.deltaTime);
}
}