Home > OS >  Seconds being skipped in IEnumerator. Unity2D
Seconds being skipped in IEnumerator. Unity2D

Time:11-21

I have a script for a 2d object in unity. The object is a security camera. I want it to go from 0 to 90 and 90 to 0 in a 90 degree arch in a controlled manner. At the ends of the arch I want it to stop for four seconds. `

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SecurityCameraMovement : MonoBehaviour
{
    public float turnspeed = 0.5f;
    private float angle;
    Transform camera;
    private bool stop = false;
    void Start()
    {
        camera = gameObject.GetComponent<Transform>();
    }

    IEnumerator movement()
    {
        angle = camera.eulerAngles.z;
            if(angle > 90){
                stop = true;
                yield return new WaitForSeconds(4);                
            }

            if(angle < 1){
                stop = false;
                yield return new WaitForSeconds(4);                
            }

                if(angle > 0 && angle < 90 & stop == false){
                    transform.rotation = Quaternion.Euler(0,0, angle   turnspeed);
                    yield return new WaitForSeconds(1);                
                } 

                    if(stop == true){
                        transform.rotation = Quaternion.Euler(0,0, angle - turnspeed);
                        yield return new WaitForSeconds(1);                
                    }   
    }

    void Update()
    {
        StartCoroutine(movement());

    }

}

`

Security Camera with hingejoint to show desired arch

I'm not quite sure how INumerator works. Currently, the camera goes from 0 degrees to 90 degrees and vice versa but very fast and with no breaks. I've looked at other problems similar but couldn't find a solution. I'm grateful for any help. Thanks!

CodePudding user response:

I believe this is because you are Starting the coroutine with the Update function. Many Enumerator instances can run at the same time. Every time the StartCoroutine function is called, a new Enumerator instance starts. I assume that these multiple instances are all adding to the angle every tick, making the speed very fast.

A solution could be to run StartCoroutine inside the Start function and just loop forever in the Enumerator (Either using while loops or running StartCoroutine inside the Enumerator). An example would be this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SecurityCameraMovement : MonoBehaviour
{
    public float turnspeed = 0.5f;
    private float angle;
    Transform camera;
    private bool stop = false;
    void Start()
    {
        camera = gameObject.GetComponent<Transform>();
        StartCoroutine(movement());
    }

    IEnumerator movement()
    {
        angle = camera.eulerAngles.z;
        if(angle > 90){
            stop = true;
            yield return new WaitForSeconds(4);                
        }
        if(angle < 1){
            stop = false;
            yield return new WaitForSeconds(4);                
        }

        if(angle > 0 && angle < 90 & stop == false){
            transform.rotation = Quaternion.Euler(0,0, angle   turnspeed);
            yield return new WaitForSeconds(1);                
        } 

        if(stop){
            transform.rotation = Quaternion.Euler(0,0, angle - turnspeed);
            yield return new WaitForSeconds(1);                
        }   
        StartCoroutine(movement());
    }

}

NOTE: This is not a good way of handling basic motion like this. It is better practice to just use the Update function. You can keep timers by using Time.deltaTime or similar functions.

  • Related