- Home /
Camera smooth movement from ponint A to B
Hello, I have a problem with my code. I want to make smooth movement from PointA to PointB (comments in the code shows which is which) and I don't know how to do this.
using UnityEngine; using System.Collections;
public class CameraMovement : MonoBehaviour {
public Transform projectile; //followed object
public Transform farLeft; //camera border marker
public Transform farRight; //camera border marker
void Start () {
Vector3 StartPosition = transform.position;
}
//In Update Camera is following object from point A (StartPosition) to point B.
void Update () {
Vector3 newPosition = transform.position;
newPosition.x = projectile.position.x;
newPosition.x = Mathf.Clamp (newPosition.x, farLeft.position.x, farRight.position.x);
transform.position = newPosition;
//It stops on farRight point
}
//Function used from other script
public void CameraChanger(){
//I want this function to slowly and smoothly take my camera
//from point B (where we are now) to point A (StartPosition)
//But I don't know how to do this
}
make 'StartPosition' a global variable as at the moment it is only accessable from Start (move it to the top with your public Transform variables). The you can use Vector3.Lerp or Vector3.$$anonymous$$oveTowards to move from transform.position to StartPosition
It doesn't work properly :/
It works but all unity freezes, console too and then, when transform.position reaches StartPoint (I know it happens because i have Debug.Log) everything get back to normal, unity unfreees.
public class Camera$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public Transform projectile; //followed object
public Transform farLeft; //camera border marker
public Transform farRight; //camera border marker
private float speed = 0.001f;
private float lerp$$anonymous$$oving = 0.0f;
private Vector3 StartPosition;
void Start () {
Vector3 StartPosition = transform.position;
}
//In Update Camera is following object from point A (StartPosition) to point B.
void Update () {
Vector3 newPosition = transform.position;
newPosition.x = projectile.position.x;
newPosition.x = $$anonymous$$athf.Clamp (newPosition.x, farLeft.position.x, farRight.position.x);
transform.position = newPosition;
//It stops on farRight point
}
//Function used from other script
public void CameraChanger(){
while (transform.position != StartPosition) {
lerp$$anonymous$$oving += Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, StartPosition, speed*lerp$$anonymous$$oving);
Debug.Log ("Backtrack: " + transform.position);
}
Debug works well, but unity just freezes
The reason is that your while loop does not allow your frame to progress until it has finished e.g. when transform.position == StartPosition, so nothing will happen, and then you will see everything happen at once once the while has finished. You should use a coroutine so that you can see the progress at each stage of the while loop. Another problem you might find with your current code is that you should ~never~ really compare floating point numbers and hence you should not directly compare to Vector3's as you are doing with (transform.position != StartPosition)
I think a better approach might be:
public IEnumerator CameraChanger(){
float progress = 0;
while (progress <= 1) {
transform.position = Vector3.Lerp(transform.position, StartPosition, progress);
progress += speed*Time.deltaTime;
Debug.Log ("Backtrack: " + transform.position);
yield return null;
}
transform.position = StartPosition;
}
And you should call it as:
StartCoroutine(CameraChanger());
Hope that helps!
Scribe
How to call Coroutine from other script? With normal functions it looks like:
Camera$$anonymous$$oveScript.CameraChanger();
but how should it looks like with Coroutine?
got it by
public void CallCameraChanger() {
StartCoroutine(CameraChanger());
}
It works! Awesome!! Thank you very much!