- Home /
Change position - cameras - slider
Hello everyone!
I have some probelm, because I am begginer in scripting.
I have attached two cameras to one GameObject, both are childs for GameObjcet in hierarchy. I want to add script to GameObject, which allows me to change positions each of cameras simultaneously thanks to one slider.
For example: I move slider, one of cameras move ten units to the right and simultaneously second camera move the same number of units to the left.
Can anyone show me script, which let me do this thing?
My script for now:
using UnityEngine; using System.Collections;
public class hSlider : MonoBehaviour {
private float hSliderValue = 0.0f;
void OnGUI () {
hSliderValue = GUI.HorizontalSlider (new Rect (25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
}
void Update() {
transform.Translate (Vector3.right * hSliderValue * Time.deltaTime);
}
}
Thanks a lot!
Answer by hav_ngs_ru · Nov 05, 2014 at 02:38 PM
private float hSliderValue = 0.0f;
// setup this links in editor, just drag your cameras from scene
public Camera leftCamera;
public Camera rightCamera;
void OnGUI () {
hSliderValue = GUI.HorizontalSlider (new Rect (25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
}
void Update() {
//transform.Translate (Vector3.right * hSliderValue * Time.deltaTime);
// Ive commented it because here your slider sets VELOCITY of camera movement, not POSITION
//if you want to slider sets POSITION - dont translate cameras every update
leftCamera.transform.localPosition = new Vector3(
leftCamera.transform.localPosition.x,
hSliderValue,
leftCamera.transform.localPosition.z);
rightCamera.transform.localPosition = new Vector3(
leftCamera.transform.localPosition.x,
-hSliderValue,
leftCamera.transform.localPosition.z);
// additionally you may have some multiplier for hSliderValue here to make camera move slower or faster
}
2 casies:
1.
//if you want to slider sets POSITION - dont translate cameras every update
leftCamera.transform.position.localPosition.y = hSliderValue;
rightCamera.transform.position.localPosition.y = -hSliderValue;
UNITY show me the error next to this lines: Element UnityEngine.Vector3 doesn't contain "localPosition" definition, and UNITy doesn't found expanding method "localPosition" which for first argument recive argument "UnityEngine.Vector3" type.
2.
//if you want to slider sets POSITION - dont translate cameras every update
leftCamera.transform.position.localPosition.y = hSliderValue;
rightCamera.transform.position.localPosition.y = -hSliderValue;
UNITY show me the error next to this lines: You can't modify return value "Unity.Engine.Transform.LocalPosition, because it is not a variable.
Access either the transform position variable OR the localPosition variable, not both.
http://docs.unity3d.com/ScriptReference/Transform.html
http://docs.unity3d.com/ScriptReference/Transform-position.html
http://docs.unity3d.com/ScriptReference/Transform-localPosition.html
arrh, sorry! of cause transform.localPosition, not transform.position.localPosition