【问题标题】:How to solve the error with IEnumerator(Unity, C#)?如何使用 IEnumerator(Unity, C#) 解决错误?
【发布时间】:2020-01-01 07:48:00
【问题描述】:

我需要让我的角色墙运行,但IEnumerator 有代码问题

这是Unity 4.5.x,用 C# 编写的代码

using UnityEngine;
using System.Collections;

public class Moving : MonoBehaviour {

public float speed = 6.0F;
public float jumpSpeed = 8.0F; 
public float gravity = 20.0F;
public float runTime = 1.0f;
private Vector3 moveDirection = Vector3.zero;
private bool isWallL = false;
private bool isWallR = false;
private RaycastHit hitL;
private RaycastHit hitR;
private int jumpCount = 1;

IEnumerator afterRun() {
    yield return new WaitForSeconds (runTime);
    isWallL = false;
    isWallR = false;
    gravity = 20;
}
void Update() {
    CharacterController controller = GetComponent<CharacterController>();

    if (controller.isGrounded) {
        jumpCount = 0;
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }

    if (Input.GetKeyDown (KeyCode.Space) && !controller.isGrounded && jumpCount <= 1) {
        if (Physics.Raycast (transform.position, -transform.right, out hitL, 1)){
            if (hitL.transform.tag == "Wall"){
                isWallL = true;
                isWallR = false;
                jumpCount = 1;
                gravity = 0;
                StartCoroutine (afterRun);
            }
        }
        if (Physics.Raycast (transform.position, transform.right, out hitR, 1)){
            if (hitR.transform.tag == "Wall"){
                isWallL = false;
                isWallR = true;
                jumpCount = 1;
                gravity = 0;
                StartCoroutine (afterRun);
            }
        }
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
    }
}

预计没有错误,但我有两个:

错误 CS1502:UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)' 的最佳重载方法匹配有一些无效参数”和“错误 CS1503:参数 #1' 无法将方法组的表达式转换为 System.Collections 类型.IEnumerator'。

【问题讨论】:

  • CharacterController 控制器 = GetComponent();无需在更新中调用它,您只需在 Start 方法中调用一次即可:)
  • @phantasm 实际上不是you can,而是you should! ;) GetComponent 性能非常强
  • afterRun 是一个函数而不是 IEnumerator ,所以应该使用 StartCoroutine(afterRun())

标签: c# unity3d ienumerator


【解决方案1】:

为什么不这样:

private IEnumerator coroutine;

然后设置它并调用它:

coroutine = afterRun();
StartCoroutine(coroutine);

【讨论】:

    【解决方案2】:

    afterRun 在您的代码中是一个函数,但您调用它时不带括号。所以:

    StartCoroutine (afterRun());
    

    例如:

    namespace someNamespace
    { 
        public class SomeClass
        {
            IEnumerator afterRun()
            {
                yield return new WaitForSeconds(3);            
            }
    
            public void Test(IEnumerator enumerator)
            {
                while(enumerator.MoveNext())
                {
                    //do some work
                }
            }
    
            public void YoureCode()
            {
                Test(afterRun());
            }
        }
    
        public class WaitForSeconds
        {
            public WaitForSeconds(int a)
            {            
            }
        }
    }
    

    有关详细信息,请参阅 enter link description here

    【讨论】:

    • 嘿,你的帖子之前已经不错了......现在你添加了一些相当无意义的例子;)我不会那样做,因为它已经很清楚该做什么和在哪里,而是链接到@987654322 @改为
    【解决方案3】:

    按照 Unity coroutines 的文档,看来协程函数必须调用为 StartCoroutine ("afterRun");

    【讨论】:

    • 两者都是有效答案(参见StartCoroutine),但我更喜欢使用Maxim 的解决方案,而不是使用string 参数,因为它们很容易出错。尤其是当您开始想要将多个参数传递给 IEnumerator 时;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多