4

In Unity 3D I have a series of camera movements, but some don't successfully complete. Instead of the camera coming to rest, it ends up vibrating around the target location.

I have the video of my problem here so you can see what I mean:

Youtube video of my problem

The final two transitions demonstrate it very clearly of you want to jump straight to them, but the first but shows how I have the transitions set up.

Can you let me know how to stop this oscillation?

Camera is shaking because it is updating it's position Constantly the ( flag boolean must be false ).

Here is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class camMOVE : MonoBehaviour {
    public Transform  handleview;
    public Transform pressureview;
    public Transform wallview;
    public Transform sechandleview;
    public Transform pressuretwoview;
    public Transform switchview;

    public GameObject handlebtn;
    public GameObject pressurebtn;
    public GameObject wallbtn;
    public GameObject handletwobtn;
    public GameObject pressuretwobtn;
    public GameObject switchbtn;


    public float transitionSPEED;
    Transform currentVIEW;
    public bool flag = false;
    public bool isReached = false;
    Vector3 currentangel;
    public List<GameObject> modelparts;

    private void Start(){
        handlebtn.SetActive (true);
        pressurebtn.SetActive (false);
        wallbtn.SetActive (false);
        handletwobtn.SetActive (false);
        pressuretwobtn.SetActive (false);
        switchbtn.SetActive (false);

        foreach (GameObject obj in modelparts) {
            obj.GetComponent<BoxCollider> ().enabled = false;
        }
    }

    private void Update(){
        if (flag) {
            transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
            //transform.position = Vector3.MoveTowards (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

            //for camera rotation
            currentangel = new Vector3 (Mathf.LerpAngle (transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
            Mathf.LerpAngle (transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
            Mathf.LerpAngle (transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

            transform.eulerAngles = currentangel;
        }
    }

    public void Handleview(){
        currentVIEW = handleview;
        handlebtn.SetActive (false);
        flag = true;
    }

    public void Pressureview(){
        currentVIEW = pressureview;
        pressurebtn.SetActive (false);
        flag = true;
    }

    public void Wallview(){
        currentVIEW = wallview;
        wallbtn.SetActive (false);
        flag = true;
    }

    public void Secondhandleview(){
        currentVIEW = sechandleview;
        handletwobtn.SetActive (false);
        flag = true;
    }

    public void Pressuretwoview(){
        currentVIEW = pressuretwoview;
        pressuretwobtn.SetActive (false);
        flag = true;
    }

    public void Switchview(){
        currentVIEW = switchview;
        switchbtn.SetActive (false);
        flag = true;
    }

}

migrated from movies.stackexchange.com Nov 28 '18 at 8:32

This question came from our site for movie and tv enthusiasts.

0

I don't understand where you are looping between the different objects you want the camera to move to. I would try to go through all these steps:

  1. setup the elements you want to be filmed, and add them to an array.
  2. Create a flow. Call a method that will start lerping the camera to the next object (transform), and if the camera arrives the next object, call another method that will wait for 1 second and then call the method from the beginning again. Here some pseudocode

    IEnumerator startAnimation(){ 
      yield return new WaitForSeconds(.1f);
      // choose next object to move to
      // change flag to yes
    }
    
    void Update{
      if( flag ){
        // detect if camera is moving near the next transform/position
        // call delayAnimation();
        // flag = false to stop animation
      }
    }
    
    IEnumerator delayAnmiation(){ 
      yield return new WaitForSeconds(.1f);
      startAnimation();
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy