In Unity how do i make the camera follow a character in the y axis..?
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-image
Answer by LiamSorta · Sep 16, 2015 at 12:17 PM
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.
i tried your script but it got Exception- "Object reference not set to instance of object".
Finally got it to work..!! Thanks..!! What if i want to move the camera in the positive y axis only not in negative y axis..with the ball..?
Sorry, been busy with moving into Uni and whatnot! So for this (if I'm understanding what you mean correctly) you could either use a clamp or just check the ball position.
$$anonymous$$g
if(yourGameObject.transform.position.y > 0)
{
transform.position = new Vector3(transform.position.x, yourGameObject.y, -10);
}
else
{
transform.position = new Vector3(transform.position.x, 0, -10
}
Let me know if it works for you!
Answer by Seyren · Sep 16, 2015 at 03:34 PM
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
This will not work; in fact, it' won't even compile and throw an error (in C#, that is). transform.position, eulerAngles and similar Vector3 variables always have to be assigned as a whole. Assigning single x, y, z values doesn't work.
True, forgot to apply the Vector3, i'm going to adjust it.
Answer by Cherno · Sep 14, 2015 at 11:11 PM
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;
}
}
@Cherno - Thanks for the response..!! Sorry I'm new to coding and unity i added camera as a child to my character and your code to the camera. The camera moves with the character along the scene and when the character turns left the camera moves in the x axis with the ball which is what i dont want.The character is centered with the camera and hence exposing -x and x planes while turning left and right as in the attachment.. .Could u elaborate in a detailed way please..!!
I'm new to coding too but doesn't removing the camera from the character as a child fix the problem?
If your camera is a child of the player object, then following is handles automatically of course; The transformations that are applied due to movement and rotation of the parent object need to be overridden, which is a bit more complicated. I don't even know if it's possible, but you can try and put the code I posted above into the LateUpdate() function, which, as the name says, is called later and it might be called after parent-child transformations are applied:
public float $$anonymous$$_y = -2f;
public float camPos_x = 4f;
void LateUpdate() {
Vector3 pos = transform.position;
if(pos.y < $$anonymous$$_y) {
pos.y = $$anonymous$$.y;
}
pos.x = camPos_x;
transform.position = pos;
}
If it doesn't work, unparent the camera and try this code:
public Transform playerTransform;
public float $$anonymous$$_y = -2f;
void Update() {
Vector3 pos = transform.position;
pos.y = playerTransform.y;
if(playerTransform.y < $$anonymous$$_y) {
pos.y = $$anonymous$$.y;
}
transform.position = pos;
}
what do u mean by "$$anonymous$$.y" i'm getting errors....
I meant $$anonymous$$_y, the variable that has been declared in the script's main body.
Your answer
![](https://koobas.hobune.stream/wayback/20220612033742im_/https://answers.unity.com/themes/thub/images/avi.jpg)