- Home /
Move camera with inspector slider and include it's current location?
While playing I am moving the camera (z) with a slider in the inspector. I want the slider to take into account the current camera z position but when I do that, the slider no longer moves the camera. Here is the code that does move the camera but the camera z position is set to 0.
using UnityEngine;
using System.Collections;
public class Main_FadeWithCameraDistance : MonoBehaviour
{
[Range(-100, 100)]
public float cameraZSlider;
public Vector3 cameraPos;
void Update()
{
CameraLocation();
}
void CameraLocation()
{
cameraPos.x = Camera.main.transform.position.x;
cameraPos.y = Camera.main.transform.position.y;
cameraPos.z = cameraZSlider;
Camera.main.transform.position = cameraPos;
}
}
If my camera is at -20 z, I want the slider to be at -20 and the camera at -20 when hit play and for the slider to still work. cameraZSlider = Camera.main.transform.position.z doesn't work since I can no longer use the slider.
Answer by browne11 · Feb 27, 2018 at 07:49 PM
@OreoSplitter You need to set your slider z to what the camera has? Then use the below script. This is what the start and awake function are used for.
using UnityEngine;
using System.Collections;
public class Main_FadeWithCameraDistance : MonoBehaviour
{
[Range(-100, 100)]
public float cameraZSlider;
public Vector3 cameraPos;
void Start(){
cameraZSlider = Camera.main.transform.position.z;
}
void Update()
{
CameraLocation();
}
void CameraLocation()
{
cameraPos.x = Camera.main.transform.position.x;
cameraPos.y = Camera.main.transform.position.y;
cameraPos.z = cameraZSlider;
Camera.main.transform.position = cameraPos;
}
}
Yeah I got it working yesterday right after waking up and thought "I've been working on this for 2 days and thats all I had to do?" Oh well, lesson learned.
Answer by WbrJr · Feb 25, 2018 at 05:55 PM
Hello there,
I would recommend to write everything in "FixedUpdate" function. It is updating constantly and there will be no lags.
And calling the "CameraLokation"-function every frame in the update methode is quiet unefficient, put it directly in the Update or FixedUpdate function.
Now to your question:
You want to control the Z-position of your camera with a slider in your inspector, right?
And the script is sitting on the camera you want to move?
if so, here is the script I would use:
[Range(-100, 100)]
public float camPosZ;
void FixedUpdate()
{
Vector3 camPos = transform.position;
camPos.z = camPosZ;
Camera.main.transform.position = camPos;
}
Why do you want the Vector 3 to be public? if you want to move the camera via another script, just use "Main.Camer.transform.position".
And you cant move the camera in Playmode in unity, if you dont use the slider you made.
I hope I could help, and it works..
Have a nice Day and good luck,
WbrJr
That script isn't any better then the one I posted. If my camera is at -20 z, it becomes 0 when I press play. I asked "If my camera is at -20 z, I want the slider to be at -20 and the camera at -20 when hit play and for the slider to still work. "
Thanks for the try but this script doesn't fix my problem. The whole issu is getting the slider to see the current camera position AND still function.