-1

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;
}

}

Alex Myers
  • 6,196
  • 7
  • 23
  • 39
Nouman Khan
  • 49
  • 2
  • 11

2 Answers2

0
if (flag == true)
{
    transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
    if(Mathf.Approximately((transform.position-currentVIEW.position).sqrmagnitude, 0f))
    {
        transform.position = currentVIEW.position;
        transform.rotation = currentVIEW.rotation;
        flag = false;
        return;
    }
....
Daveloper
  • 748
  • 4
  • 13
  • let me try this then i will tell u – Nouman Khan Nov 27 '18 at 15:13
  • @developer what is sqrmagnitude? – Nouman Khan Nov 27 '18 at 15:17
  • @developer this does not work its still updating and camera is still shaking – Nouman Khan Nov 27 '18 at 15:21
  • sqrmagnitude is less expensive to compute (it's the magnitude squared, so it's just x X x + y X y + z X z, instead of the squared root of it) If your camera is shafking it means that your transform.position gets changed constantly. try replacing Mathf.Epsilon with 0.01f. the idea being to make it stop at some point. I'm confused that it is shaking at all, because Lerp should get it asymptotically to your final position, that should not "shake" :/ – Daveloper Nov 27 '18 at 15:30
  • @develper yea its doing the job but using this 0.01f the camera stops instantly and i have some other conditions in my game which are not working by using this method please tell any other method please – Nouman Khan Nov 27 '18 at 15:36
  • instead of using Mathf.Epsilon you can use Mathf.Approximately – AresCaelum Nov 27 '18 at 15:41
  • https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html also look at `transform.position.distance(currentVIEW.position)` with mathf.approximately – AresCaelum Nov 27 '18 at 15:41
  • @daveloper i have another code of doing same thing but using coroutines but in that code when camera lerp once from one position to another position then it not lerp again – Nouman Khan Nov 27 '18 at 15:42
  • @Eddge i put mathf.Approximately and this error comes " error CS0019: Operator `<' cannot be applied to operands of type `float' and `method group' " – Nouman Khan Nov 27 '18 at 15:43
  • I will write an answer with an example. – AresCaelum Nov 27 '18 at 15:44
  • @Eddge yes please use my code to write the answer please – Nouman Khan Nov 27 '18 at 15:45
  • @NoumanKhan: if(Mathf.Approximately((transform.position-currentVIEW.position).sqrmagnitude, 0f) – Daveloper Nov 27 '18 at 15:48
  • @Daveloper ur this line of code is working but when i call the "Wallview()" function the camera starts moving and rotating in another way randomly – Nouman Khan Nov 27 '18 at 16:18
  • @Daveloper and if i use vector3.movetowards then its working but if i use vector3.lerp then its not working – Nouman Khan Nov 27 '18 at 16:22
  • @NoumanKhan There might be a misunderstanding of how `Vector3.Lerp(Vector3, Vector3, float)` and `Vector3.MoveTowards(Vector3, Vector3, float)` work. The last float parameter in `Vector3.Lerp` is a range of [0, 1], so a percentage of the distance. `Vector3.MoveTowards` moves the object a number of units closer. If you attempt to use the same float between the two, `Vector3.Lerp` might look like it does not move. – Eliasar Nov 27 '18 at 19:54
  • shouldn't the lerp have a smoothing effect? you always linearly interpolate between the current and final position, updating the current one for the next lerp, hence the amount of change diminished(decelerates) so it doesn't feel so linear... – Daveloper Nov 28 '18 at 09:11
  • @Daveloper it will at first, however you never really arrive at your destination. – AresCaelum Nov 28 '18 at 13:25
  • that's why at a certain delta you just snap and stop lerping – Daveloper Nov 28 '18 at 13:34
0

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:

 if (flag == true) {
        // This will move you right to the location you want.
        transform.position = Vector3.MoveTowards (transform.position, currentVIEW.position,  Time.deltaTime * transitionSPEED);  

       if(Mathf.Approximately(Vector3.Distance(transform.position, currentVIEW.position), 0f))
       {
           flag = false;
       }
    } // This is the end bracket for the if statement.

Constantly changing the starting Lerp starting location can give incorrect results, and timings.

AresCaelum
  • 1,558
  • 1
  • 13
  • 18
  • I inverted the order of MoveTowards, Editted the answer to fix that, so it should be `Vector3.MoveTowards (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);` – AresCaelum Nov 27 '18 at 15:52
  • let me try this – Nouman Khan Nov 27 '18 at 15:54
  • im getting these errors error CS1061: Type `UnityEngine.Vector3' does not contain a definition for `distance' and no extension method `distance' of type `UnityEngine.Vector3' could be found. Are you missing an assembly reference? and this one error CS1501: No overload for method `Approximately' takes `1' arguments – Nouman Khan Nov 27 '18 at 16:13
  • @NoumanKhan I fixed the typos, Vector3.Distance has a capital D, I wrote this all on here so yea. in regards to approximate I fixed that line above. – AresCaelum Nov 27 '18 at 16:26
  • now getting this error " error CS1501: No overload for method `Distance' takes `1' arguments " – Nouman Khan Nov 27 '18 at 16:35