Hi there!
I created camera follow class to follow player movement based on the y axis.
below is my code .
public class CameraFollow : MonoBehaviour {
public Transform target;
public float topLimit=10.0f;
public float bottomLimit=-10.0f;
public float followSpeed=0.5f;
void LateUpdate()
{
if (target != null)
{
Vector3 position = this.transform.position;
position.y = Mathf.Lerp(position.y,target.position.y,followSpeed);
position.y = Mathf.Clamp(position.y, bottomLimit, topLimit);
}
}
}
I have tried to run the game, and no changes in camera , although player has moved, camera doesn’t work or it doesn’t do anything to follow the player.
if you can help my question i’m really appreciate that, Thanks.
Eoku_1
2
You need to reassign the position you calculated back to the transform, or else nothing will actually move.
public Transform target;
public float topLimit=10.0f;
public float bottomLimit=-10.0f;
public float followSpeed=0.5f;
void LateUpdate()
{
if (target != null)
{
Vector3 position = transform.position;
position.y = Mathf.Lerp(position.y,target.position.y,followSpeed);
position.y = Mathf.Clamp(position.y, bottomLimit, topLimit);
transform.position = position; // <-- This line is what your code is missing. XD
}
}
Side note: You don’t need to use “this”. It’s already implied if you just write “transform”.