Hello i have a code which moves my main camera to other game objects positions when i press button like if i press button 1 it will move towards object 1 position same for button 2 and 3. Now in my code i have a boolean named as flag which is true in update function then i have multiple public functions for multiple buttons now when i press any button the boolean becomes true and remains true which causes camera to shake because boolean is continously updating please tell me a way that when my camera reaches it final position the flag boolean becomes false and when again i press another button i will become true again here is my 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 GameObject parentobj;
Animator anim;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = 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);
anim = parentobj.GetComponent<Animator> ();
anim.SetBool ("start", true);
//currentVIEW = handleview;
foreach (GameObject obj in modelparts) {
obj.GetComponent<BoxCollider> ().enabled = false;
}
}
private void Update(){
if (flag == true) {
transform.position = Vector3.Lerp (transform.position,
currentVIEW.position, Time.deltaTime * transitionSPEED);
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;
}
}
Alright, so some of the issues I am seeing is you are using lerp incorrectly, lerp is a linear interpolation point a and b at this ratio. The purpose of this gets defeated if you are constantly changing the location of pointA. You should either store your starting point when moving, and lerp between the starting point and end point. or b use
MoveTowards
.Here is an example of MoveTowards and using Mathf.Approximately:
Constantly changing the starting Lerp starting location can give incorrect results, and timings.