- Home /
Move GameObject between two points using Scrollbar
Hey Unity Pros,
I want to make a UI Scrollbar move my GameObject (in this case the Camera) between two points in 3D space. So for example if scrollbar.value is 0 than the Camera is at point A and if the value is 1 than its at point B. And also everything in between.
I'll look forward to your help and thanks in advance!
Answer by Jessespike · Apr 07, 2016 at 06:01 PM
Use Vector3.Lerp to move the camera's position between two points.
public Vector3 pointA, pointB;
public float lerpAmt; // replace this with scrollbar.value
void Update () {
Camera.main.transform.position = Vector3.Lerp(pointA, pointB, lerpAmt);
}
Oh well that's actually much easier than my code sample :)
Hey Jessespike,
Ive tested the script and it works just as I wanted it to be!! Thank you for helping me out here!! :DD
Thanks. This is much more concise than I thought, didn't think it would be so simple. $$anonymous$$any other uses for this too.
Answer by Rob2309 · Apr 07, 2016 at 06:04 PM
You can just make a script which polls the state of the scrollbar in every update() if this is your problem.
[SerializeField]
private Vector3 posA;
[SerializeField]
private Vector3 posB;
void Update()
{
float scrollBarValue = ...; //I have forgotten how to poll this sorry :D
target.transform.position = posA + ((posB - posA) * scrollBarValue);
}