-1

Please watch this video and tell me a fix. Thanks.

The boolean flag must become false when my camera reaches its final position. Currently, the flag becomes true and remains true. I need to find a way where when my camera moves to its first destination the flag becomes false and so on.

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


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

    }
}
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
Nouman Khan
  • 49
  • 2
  • 11
  • All you need is TWEENG - one line of code: https://stackoverflow.com/a/37228628/294884 – Fattie Nov 28 '18 at 05:52
  • Instead of doing it in Update, or Coroutines or that Tweeng thing, your question is a perfect example of when to start using Unities [Animator](https://unity3d.com/de/learn/tutorials/topics/animation/animator-component?playlist=17099) and a StateMachine – derHugo Nov 28 '18 at 06:53

2 Answers2

1
  1. you're not setting it to false anywhere

  2. you really don't do this in Update - you will have to simply familiarize yourself with using a coroutine

It is then very easy.

The basic form is

In your code ..

StartCoroutine(MoveCamera());

and then

private IEnumerator MoveCamera() {
  
  while ( not at the new position ) {

    move the object a little
    yield return null;
  }
  set object to final exact position
}

You'll find 1000 examples online! As a rule, never use Update in Unity.


In practice, just use Tweeng ...

Once you're comfortable with this. You can just use Tweeng.

Tweeng:

https://stackoverflow.com/a/37228628/294884

in your example, you would simply say

StartCoroutine( 3f.Tweeng(
     (p)=>transform.position=p,
     fromPosition,
     toPosition) );

That would do it in 3 seconds (ie, 3f );

Enjoy

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • I googled `Tweeng` but couldn't find anything about it .. do you mean [`Tween`](http://wiki.unity3d.com/index.php/Tween)? – derHugo Nov 28 '18 at 06:13
  • ? hi Der - the link is RIGHT THERE. (definitely not Tween, that is a nightmare.) – Fattie Nov 28 '18 at 06:36
  • link is RIGHT THERE in the answer, @derHugo ! :) https://stackoverflow.com/a/37228628/294884 – Fattie Nov 28 '18 at 06:36
  • This leads to another question and the link in that one to another question ... so it's basically something you came up with? – derHugo Nov 28 '18 at 06:48
  • hey DH - I can't really remember who started it. are you old enough to have been on the answers.unity site (which has largely collapsed today). anyway, you need some sort of extension to tweeng, it's usually not worth writing out a coroutine each time.. – Fattie Nov 28 '18 at 07:06
  • I'm still on it ;) but I see .. it just sounded like kind of an "official" available package or something and I wondered why I couldn't find anything like that. – derHugo Nov 28 '18 at 07:09
  • gotchya. I guess it's just the verb of Tween ("tween" is just a term from animation (like, Disney etc) before computers). (Verb .. adjective .. I'm not quite sure! Like jump / jumping .. something like that :) – Fattie Nov 28 '18 at 07:10
  • Thanks for the explenation anyway. It looks kind of cool but I see one drawback ... how would you **stop** a specific coroutine than? – derHugo Nov 28 '18 at 07:16
  • I also wouldn't generalize `never use Update in Unity` – derHugo Nov 28 '18 at 07:21
  • with StopCoroutine ? I was specifically addressing the OP here - OP should never use Update at this stage. you or I can use Update :) – Fattie Nov 28 '18 at 07:35
  • What do you base your recommendation on not using Update on? Because I've been doing this since Unity 2.7 and Update has always worked better for me. You have way too little control over coroutines. – Arshia001 Nov 28 '18 at 09:58
  • hi @Arshia001 (1) it's way too messy to use Update (as can been seen by the OP's problems here) (2) you have perfect control over coroutines. (3) I can't see any reason it would work "better" - how do you mean? (4) If (for some reason) you want to code inside Update, a neat idea is just launch another component which does whatever it is you're animating, and then the component destroys when finished – Fattie Nov 28 '18 at 10:14
  • It's a good example of the utterly appalling example code Unity put out in the early days. – Fattie Nov 28 '18 at 10:15
  • I get that Unity tried to simplify things with coroutines, but you can't code any complicated logic like that. What if, for example, you wanted to change the camera's target midway through the current move? What if you wanted to change it every frame? Now I'm not suggesting that you should put all logic *directly* inside Update, but having a coroutine per movement is simply a bad and improper encapsulation. – Arshia001 Nov 28 '18 at 11:35
  • And by the way, Unity's initial over-simplified yet partially inappropriate API is the reason why so many bad games are made. Developers tend to like unity, but take a look at Steam. I actually once saw a comment saying *Life is Strange: Before the Storm* was probably bad ***because*** it was made with Unity (great game though it is). See where this is going? – Arshia001 Nov 28 '18 at 11:42
  • (regarding coroutines) Heh! no offense, that's totally wrong. It's commonplace that the target of an animation (say) is changing constantly. To keep to the point, the OP's approach here is horrendous! Enjoy your coding! – Fattie Nov 28 '18 at 11:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184398/discussion-between-arshia001-and-fattie). – Arshia001 Nov 28 '18 at 11:43
  • Unity itself is not crap. People use it in crappy ways. Given a capable developer, it's actually a really powerful tool, specially for mobile games. – Arshia001 Nov 28 '18 at 11:47
  • I mean, they *still* don't understand "networking". (Apparently they are gonna try yet again, fair enough.) Heh! – Fattie Nov 28 '18 at 12:13
0

Basically, I agree with the above answer by Fattie. You could just use a Coroutine as shown below

IEnumerator PerformCameraMovement(Transform transformToMove,Vector3 fromPos,Vector3 toPos,float time)
    {
        float i = 0;
        float rate = 1 / time;
        while (i<1)
        {
            i += Time.deltaTime * rate;
            transformToMove.position = Vector3.Lerp(fromPos,toPos,i);
            yield return 0;
        }
    }

here in your case, transformToMove is the transform of your camera, fromPos is the position from where a camera should start transitioning, toPos is the position to where the camera should reach and finally time is the amount of time required to transition the camera fromPos to toPos

Coroutines work just like a thread.

Just call this Coroutine by using StartCoroutine(PerformCameraMovement(cameraTransform,cameraTransform.position,Vector3.zero,2.0f));

and placing it wherever required.

Now in the above case, the camera would move from its original position to Vector3.zero (origin) in exactly 2 seconds.

I would advise avoiding the use of Update for transitioning. It just won't work.

If you need a deeper understanding of coroutines just go through this blog below http://www.theappguruz.com/blog/how-to-use-coroutines-in-unity

  • `Coroutines work just like a thread.` .. well, kind of .. but it's actually everything but a thread .. all coroutines remain running in Unity's mainthread so a line that takes a while could block the UI thread / make your app hang even if you do it inside of an IEnumeraor – derHugo Nov 28 '18 at 07:18