- Home /
Why can't I change my camera's position?
This is the script I'm using to change my camera's Y position. It's supposed to take the position of the camera and move it into a lower or higher position. So why is my camera just staying in one position and shaking? Should I use Time.time? Should I use Slerp?
var lower : float;
var higher : float;
var currheight : float;
var raising : boolean = true;
var speed : float = 1f;
function Start () {
lower = GameObject.Find("Terrain").transform.position.y += 1;
higher = GameObject.Find("Terrain").transform.position.y += 1;
currheight = GameObject.FindGameObjectWithTag("cam2").transform.position.y;
}
function Update () {
if (raising){
GameObject.FindGameObjectWithTag("cam2").transform.position.y = Mathf.Lerp(currheight, lower, Time.deltaTime*speed);}
if (raising == false){
GameObject.FindGameObjectWithTag("cam2").transform.position.y = Mathf.Lerp(currheight, higher, Time.deltaTime*speed);}
}
Answer by Chris333 · Jan 29, 2015 at 10:44 PM
Hi,
dont use Find methods in the Update method. Get the reference to your camera at the Start method and use this reference in the update method instead. They are very expensive.
You could use Vector3.SmoothDamp to move objects, thats what i use to move objects all the time. http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html
That looks good for other stuff, but it also moves the x and z positions. I just want to move the y position slowly, and that's it. I tried doing something like this: Vector3(transform.position.x, from+1, transform.position.z));
so that it would keep the x and z position, but that didn't work
Answer by MESSI007 · Mar 16, 2017 at 10:22 AM
Vector3 derection you can use Camera.main.transform.Translate(derection) ; get the main camera with Camera.main and translate it with Transform.Translate
Your answer