【问题标题】:How do i fix my Unity 2D c# OnTriggerEnter2D delay?如何修复我的 Unity 2D c# OnTriggerEnter2D 延迟?
【发布时间】:2021-12-18 23:03:44
【问题描述】:

您好,我的统一 2d 游戏的传送代码有一些问题 我正在尝试为我的淡入淡出延迟我的 Teleport,但它不起作用

我也需要

other.transform.position = teleportTo.position;

不是这个,这是玩家没有传送的部分

transform.position = teleportTo.position;

完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Collider2D))]
public class TeleportTrigger : MonoBehaviour
{
    public enum TriggerType { Enter, Exit };

    //The Transform to teleport to
    [SerializeField] Transform teleportTo;

    //The filter Tag
    [SerializeField] string tag = "Player";

    //Trigger Event to Teleport
    [SerializeField] TriggerType type;


    void OnTriggerEnter2D(Collider2D other)
    {

        Time.timeScale = 0f;

        if (type != TriggerType.Enter)
        {
            return;
        }

        if (tag == string.Empty || other.CompareTag(tag))
        {
            //TP
            StartCoroutine(Wait());

            //all this \/ is not working there is no delay


            /*other.transform.position = teleportTo.position;
            Time.timeScale = 1f;
            */
        }

    }
     private IEnumerator Wait()
    {

        yield return new WaitForSeconds(5);

        transform.position = teleportTo.position;


        Time.timeScale = 1f;
    }
}```

【问题讨论】:

    标签: c# unity3d ienumerator


    【解决方案1】:

    设置后

    Time.timeScale = 0;
    

    yield return new WaitForSeconds (5);
    

    永远不会完成,因为 WaitForSeconds 取决于 Time.timeScale

    你宁愿使用WaitForSecondsRealtime

    yield return new WaitForSecondsRealtime(5);
    

    忽略时间刻度。


    对于您的其他问题,您可以将所有必需的参数传递给协程

    IEnumerator Wait(Transform targetTransform)
    {
        yield return new WaitForSecondsRealtime (5);
    
        targetTransform.position = teleportTo.position;
    
        Time.timeScale = 1f;
    }
    

    StartCorouine (Wait(other.transform));
    

    你也确定

    Time.timeScale = 0f;
    

    应该总是即使你们返回并且不触发传送?

    我宁愿把它移到协程中,这样只有当你知道之后你肯定会再次重置它时才会应用它。

    【讨论】:

      猜你喜欢
      • 2020-03-07
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      相关资源
      最近更新 更多