Search Unity

Unable to stop Camera lookAt

Discussion in 'Scripting' started by DigitalOddity, Apr 26, 2019.

  1. DigitalOddity

    DigitalOddity

    Joined:
    Oct 8, 2018
    Posts:
    10
    Hello all, I am running into quite a wall with this problem, I want to set my camera to look at an object when some action is done and then for it to no longer be locked to target and free again. In order to try and achieve this, I made a bool only set for a certain period of time and made it so the function only activates during the time the bool is true. However, this is not working and my camera remains locked on the object.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pushUpMirrors : MonoBehaviour
    6. {
    7.     public Camera mirrorcam; //You cam goes here
    8.     public Transform mirrortarget; //you target goes here
    9.     public GameObject MirrorSet;
    10.     public Animator throwUpMirrors;
    11.     private bool YourBool;
    12.     //bool blue = false;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         throwUpMirrors = GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (MirrorSet.GetComponent<openHintBox>().hasHintBoxBeenSet == true)
    23.         {
    24.             throwUpMirrors.SetTrigger("isMirrorActiveYet");
    25.             YourBool = true;
    26.             Invoke("SetBoolBack", 3);
    27.  
    28.         }
    29.  
    30.         if (YourBool ==true)
    31.         {
    32.             mirrorcam.transform.LookAt(mirrortarget);
    33.         }
    34.     }
    35.  
    36.     private void SetBoolBack()
    37.     {
    38.         YourBool = false;
    39.     }
    40. }
     
  2. supermoof

    supermoof

    Joined:
    Sep 24, 2015
    Posts:
    48
    Try this script. If it still doesn't work, the only thing I can think of is that hasHintBoxBeenSet isn't being reset, so it will keep changing YourBool back to true.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class pushUpMirrors : MonoBehaviour
    6. {
    7.     public Camera mirrorcam; //You cam goes here
    8.     public Transform mirrortarget; //you target goes here
    9.     public GameObject MirrorSet;
    10.     public Animator throwUpMirrors;
    11.     private bool YourBool;
    12.     //bool blue = false;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         throwUpMirrors = GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (MirrorSet.GetComponent<openHintBox>().hasHintBoxBeenSet == true)
    23.         {
    24.             throwUpMirrors.SetTrigger("isMirrorActiveYet");
    25.             YourBool = true;
    26.             StartCoroutine(SetBoolBack());
    27.         }
    28.  
    29.         if (YourBool)
    30.         {
    31.             mirrorcam.transform.LookAt(mirrortarget);
    32.         }
    33.     }
    34.  
    35.     IEnumerator SetBoolBack()
    36.     {
    37.         yield return new WaitForSeconds(3);
    38.         YourBool = false;
    39.     }
    40. }
     
  3. DigitalOddity

    DigitalOddity

    Joined:
    Oct 8, 2018
    Posts:
    10
    Script didn't work, but you were right! I spent I don't even know how many hours on this and I failed to realize the hasHintBoxBeenSet is being set to true every frame as that's how it functions. I thought by making an if statement within my if statement would be a workaround for that one being always true but it doesn't work that way. Rather the always true bool was constantly restarting my second if statement. So many hours wasted over such a simple thing oh well.