- Home /
How can i change a childs local position with a variable?
Hey, I'm just getting started on unity and I'm trying to make my own custom camera to learn the ropes rather then copying an already made camera, however I've ran into a problem. I'm incrementally making the script so at the moment I'm working on zoom and I've got the following set up for my camera:
CameraController (Gameobject)
|-CameraFocus (Gameobject)
|-MainCamera (camera)
The camera is intended to be like an assassins creed camera except you can zoom in and out.
The CameraController simply follows the players position.
The CameraFocus is to control the rotation and act as the "focal point" of the camera.
And the camera itself is just meant to sit inside the camera focus and only have it's z position changed which would be the distance (zoom) from the players character.
Here's the script i have so far which is attached to the CameraController:
public Transform playerTransform;
private Camera cam;
public Vector3 camDistance;
public float zoom = 5.0f;
void Start () {
playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
cam = Camera.main;
}
void Update (){
camDistance = new Vector3(0,0,-zoom);
if (cam) {
cam.transform.localPosition = camDistance;
}
}
void LateUpdate () {
if (playerTransform) {
this.transform.position = new Vector3 (playerTransform.position.x, playerTransform.position.y+1, playerTransform.position.z);
}
}
However this results in the camera sticking to the center of the CameraFocus for some reason (0,0,0 local position) but if i don't use the zoom variable and instead use an actual negative number like -10 for example it will work. However i would like to have a variable that i can change and have it zoom on the fly rather than be fixed.
I've searched and found nothing in relation to this specific case in using variables when changing a childs local position. It seems odd that it will work with actual number but not with a variable holding a number.
So how would i go about using a variable to control the local z position of my camera, would i need to take another approach?
Answer by duck · Jun 15, 2015 at 09:06 AM
Your concept is sound. You should be able to achieve what you want using the ideas in your question, you don't need another approach.
There must be some small problem with your code, or setup, that is causing the camera not to move. I suggest you double check what is happening at each line through your code by using Debug.Log to make sure your variables contain what you think they do.
For example, if the camera in your scene isn't tagged Main Camera, then line 8 above will put the value void into cam. Then because you have if (cam) { on line 13, it will silently fail giving no errors. Perhaps this is your problem. You can discover this by either removing the if statement, or tracing out the value of cam
using Debug.Log
.
Alternatively, there is a good general purpose following camera prefab in the Standard Assets, which operates in a similar way but has more features (including wall avoidance).
Thank you so much! i debug logged the cam and found that because the cam was declared as a camera it wouldn't work the same way as a gameobject would when changing the local position for some reason. So all i did was just find the camera gameobject by tag and assign its gameobject to cam which fixed it :)