Hello everyone! This is my first time building a game from scratch and I have a few questions.
The game I’m working on is a rail shooter and the issue is related to the CameraFollow script.
My problem: I have a camera that follows the player, but without leaving a predefined area (5x5). The player position is also clamped by using ‘Camera.WorldToViewportPoint’ and ‘Camera.ViewportToWorldPoint’.
I use SmoothDamp, but for some reason when the player moves in the Y-axis the Camera follows it with jitter.
Code:
public class CameraFollow : MonoBehaviour
{
public Vector3 velocity= Vector3.zero;
// some other variables
void Update()
{
FollowTarget(target);
}
void LateUpdate()
{
Vector3 localPos = transform.localPosition;
transform.localPosition = new Vector3(Mathf.Clamp(localPos.x, -limits.x, limits.x), Mathf.Clamp(localPos.y, -limits.y, limits.y), localPos.z);
}
public void FollowTarget(Transform t)
{
Vector3 localPos = transform.localPosition;
Vector3 targetLocalPos = t.transform.position;
transform.localPosition = Vector3.SmoothDamp(localPos, new Vector3(targetLocalPos.x + offset.x, targetLocalPos.y + offset.y, targetLocalPos.z + offset.z), ref velocity, smoothTime);
}
}
I tried changing the velocity and even placing the FollowTarget function inside LateUpdate and FixedUpdate (which I’m still learning) but with no success.
My questions:
1 - What could be the reason behind that movement with jitter only in Y-axis? Could it be related to the screen size (mine is 1920x1080) ?
2 - Another thing that I noticed is that when I move the player to the upper-right corner, velocity value in X is greater that Y. Why is that?
3 - As regards smoothTime, I cannot pick a value greater that 0.125 without worsen the situation (for both axis). Am I missing anything else? maybe related to the 2 last parameters in SmoothDamp
Thanks in advance!