- Home /
Deactivate Update() Function.
Hi, So I want that when the code reaches this part
"if (this.transform.localPosition == targetpos) "
stops checking Update() Function, I'm not sure if I should use the Update, but I have no idea, thank you for your help
void Update() {
if (isLookedAt)
{
transform.localPosition = Vector3.SmoothDamp (transform.localPosition, targetpos, ref velocity, smoothTime);
Debug.Log ("TestInside");
if (this.transform.localPosition == targetpos) {
Debug.Log("BUTTON HAS BEEN SELECTED!");
Answer by fafase · Aug 03, 2016 at 08:15 PM
Coroutine is more appropriate when dealing with temporary action:
IEnumerator MyCoroutine() {
while(this.transform.localPosition != targetpos){
transform.localPosition = Vector3.SmoothDamp (transform.localPosition, targetpos, ref velocity, smoothTime);
Debug.Log ("TestInside");
yield return null.
}
}
There is a chance that the check in the while never returns true because of float inaccuracy. If it is the case, check for a magnitude difference instead.
You could also use Lerp or MoveTowards which seem more appropriate for this purpose.
Thank you!! I think it is asking a lot, but you were right about float innacuracy, how can I use the magnitude difference??
I have fixed it this way:
using UnityEngine;
using System.Collections;
public class LookingAtScript : $$anonymous$$onoBehaviour {
public Vector3 targetpos;
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
public float speed = 0.1f;
public Transform target;
private bool isLookedAt = false;
public bool BridgeIsReadySir = false;
// Use this for initialization
void Start () {
}
void Update()
{
if (isLookedAt)
{
StartCoroutine (Planking (target));
}
}
// Update is called once per frame
IEnumerator Planking(Transform target)
{
while (target.transform.localPosition != targetpos)
{
if (isLookedAt)
{
transform.localPosition = Vector3.SmoothDamp (transform.localPosition, targetpos, ref velocity, smoothTime);
BridgeIsReadySir = true;
yield return null;
}
}
}
public void SetGazedAt(bool gazedAt)
{
isLookedAt = gazedAt;
}
}
I´m not sure if this is better?
while ((target.transform.localPosition - targetpos).sqr$$anonymous$$agnitude > 0.01f){}
This could do.
Your answer
![](https://koobas.hobune.stream/wayback/20220612090120im_/https://answers.unity.com/themes/thub/images/avi.jpg)