i have a character which keeps moving in the y-axis and the camera needs to be fixed to follow with the character for some extent and when the character falls down camera should freeze at the point. The attached image will make u understand better-1
If you want to follow it on a simple Axis, just attach a script to the camera where the GameObject is referenced.
Then just do in the update function:
transform.position = new Vector3(transform.position.x,ball.transform.position.y, transform.position.z);
Being ball the object for example.
Add ball as a public gameobject and reference it from the editor by dragging it from the scene and you should be good to go.
If you want to freeze at some point just add a bool to the script and done. So for example:
private bool _freeze;
public GameObject ball;
void Update()
{
if (freeze)
transform.position = new Vector3(transform.position.x, ball.transform.position.y, transform.position.z);
}
You are basically using the same position for the camera on the Y axis, which is why you are using transform.x and transform.z
There’s a few ways to go about doing this. If you want a really simple solution then you can add this script to your main camera:
Add this just above your Start() function:
[SerializeField]
GameObject player;
Then add this in your Update() function:
transform.position = new Vector3(transform.position.x,
GameObject.Find("yourcharacterobjectname").transform.position.y,
transform.position.z);
Then in the inspector drag your character onto the [None (GameObject] field in your script on the main camera.
If it was me I’d go with keeping the camera stationary and have the player jump as they need to with the background/obstacles generating downwards.
Easy. Just set the camera’s transform.position.y to whatever you like after making it follow the player.
public float min_y = -2f;
void LateUpdate() {
if(transform.position.y < min_y) {
Vector3 pos = transform.position;
pos.y = min.y;
transform.position = pos;
}
}