-2

我在 Unity 5 中收到此错误消息

错误CS1525:意外符号insert variable name', expecting

using UnityEngine;
using System.Collections;

public class jumpControll : MonoBehaviour 
{

    public bool jump;
    public float jumpHeight;



    // Use this for initialization

    public IEnumerator jumpUp (float jumpHeight) 
    {
        jumpHeight = 3.0f;
        transform.position = new Vector3(transform.position.x,
          transform.position.y + jumpHeight, transform.position.z);
    }

    public IEnumerator jumpDown (float jumpHeight) 
    {
        jumpHeight = 3.0f;
        transform.position = new Vector3(transform.position.x,
           transform.position.y - jumpHeight, transform.position.z);
    }

    public IEnumerator jumpTest()
    {
        jumpUp(float jumpHeight);
        yield return new WaitForSeconds(1);
        jumpDown(float jumpHeight);
    }

    // Update is called once per frame
    void Update () 
    {
        jump = Input.GetKey(KeyCode.Space);
        if (jump == true)
            jumpTest();
    }
}
4

2 回答 2

0

有几件事不允许您的代码编译。

首先,您指定您的jumpUpandjumpDown方法将返回一个IEnumerator但是,您不返回一个。如果你不需要在jumpUpand jumpDownthan 中等待一段时间,它们应该有一个返回类型void而不是IEnumerator

public void jumpUp (float jumpHeight) 
{
    jumpHeight = 3.0f;
    transform.position = new Vector3(transform.position.x,             
                                     transform.position.y + jumpHeight,
                                     transform.position.z);
}

public void jumpDown (float jumpHeight) 
{
    jumpHeight = 3.0f;
    transform.position = new Vector3(transform.position.x,
                                     transform.position.y - jumpHeight, 
                                     transform.position.z);
}

其次,在您的jumpTest方法中,您尝试调用jumpUpjumpDown传入您的jumpHeight变量。您不需要float在要传递的变量前面使用关键字,因为您不是在创建新变量,而是在使用已经存在的变量。

public IEnumerator jumpTest()
{
    jumpUp(jumpHeight);
    yield return new WaitForSeconds(1);
    jumpDown(jumpHeight);
}

最后,为了使IEnumerator方法正常工作,您需要使用StartCoroutine(). 例如,在您的更新方法中:

void Update () 
{
    jump = Input.GetKey(KeyCode.Space);
    if (jump == true)
        StartCoroutine(jumpTest());
}
于 2015-10-13T18:44:04.317 回答
-1
    public IEnumerator jumpUp(float jumpHeight)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y + jumpHeight, transform.position.z);
    }

    public IEnumerator jumpDown(float jumpHeight)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y - jumpHeight, transform.position.z);
    }

    public IEnumerator jumpTest()
    {
        jumpUp(3.0f);
        yield return new WaitForSeconds(1);
        jumpDown(3.0f);
    }
于 2015-10-13T18:29:05.483 回答