我在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();
}
}
答案 0 :(得分:0)
有几件事情不允许你的代码编译。
首先,您指定jumpUp
和jumpDown
方法将返回IEnumerator
但是,您不返回一个。如果您不需要在jumpUp
和jumpDown
中等待一段时间,那么返回类型应为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
方法中,您尝试调用jumpUp
和jumpDown
传递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());
}
答案 1 :(得分:-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);
}