---
title: "Corrutinas"
content_type: tutorial
url: "https://learn.unity.com/tutorial/corrutinas?language=es"
language: es
difficulty: intermediate
duration_minutes: 5
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es&version=4.x"
available_languages:
  - name: "en"
    url: "https://learn.unity.com/tutorial/coroutines.md?language=en"
  - name: "es"
    url: "https://learn.unity.com/tutorial/corrutinas.md?language=es"
  - name: "ja"
    url: "https://learn.unity.com/tutorial/coroutines-jp.md?language=ja"
  - name: "zh-cn"
    url: "https://learn.unity.com/tutorial/xie-cheng.md?language=zh-cn"
last_updated: "2026-08-04T14:21:20.000Z"
---

# Corrutinas

Aprende a crear corrutinas y a usarlas para ejecutar comportamientos complejos a lo largo del tiempo.

Cómo se crean las corrutinas y cómo se usan para lograr comportamientos complejos.

## 1. Corrutinas

[Video](http://www.youtube.com/watch?v=5L9ksCs6MbE)

 **CoroutinesExample**

```csharp
using UnityEngine;
using System.Collections;

public class CoroutinesExample : MonoBehaviour
{
    public float smoothing = 1f;
    public Transform target;


    void Start ()
    {
        StartCoroutine(MyCoroutine(target));
    }


    IEnumerator MyCoroutine (Transform target)
    {
        while(Vector3.Distance(transform.position, target.position) > 0.05f)
        {
            transform.position = Vector3.Lerp(transform.position, target.position, smoothing * Time.deltaTime);

            yield return null;
        }

        print("Reached the target.");

        yield return new WaitForSeconds(3f);

        print("MyCoroutine is now finished.");
    }
}
```

 **PropertiesAndCoroutines**

```csharp
using UnityEngine;
using System.Collections;

public class PropertiesAndCoroutines : MonoBehaviour
{
    public float smoothing = 7f;
    public Vector3 Target
    {
        get { return target; }
        set
        {
            target = value;

            StopCoroutine("Movement");
            StartCoroutine("Movement", target);
        }
    }


    private Vector3 target;


    IEnumerator Movement (Vector3 target)
    {
        while(Vector3.Distance(transform.position, target) > 0.05f)
        {
            transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);

            yield return null;
        }
    }
}
```

 **ClickSetPosition**

```csharp
using UnityEngine;
using System.Collections;

public class ClickSetPosition : MonoBehaviour
{
    public PropertiesAndCoroutines coroutineScript;


    void OnMouseDown ()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        Physics.Raycast(ray, out hit);

        if(hit.collider.gameObject == gameObject)
        {
            Vector3 newTarget = hit.point + new Vector3(0, 0.5f, 0);
            coroutineScript.Target = newTarget;
        }
    }
}
```
