如何使脚本只在一个轴上翻译?

时间:2015-09-02 00:51:25

标签: unity3d unityscript

我希望我的精灵只在轴上移动,所以我从代码中取出了x轴。但是,当我尝试在Unity中编译它时,它会返回错误:

  

Assets / Scripts / TongueMove.js(19,83):BCE0024:类型'UnityEngine.Vector2'没有与参数列表'(float)'匹配的可见构造函数。

此外,我会添加什么,以便它只会持续一段时间,然后再回到原来的位置?

#pragma strict

function Start() {
    transform.position.z = -0.5;
}

function Update () {
    if (Input.GetKeyDown ("space"))
    {
        moveTo(transform.position.y + 11.8, 20); //transform.position.y + how much I want to move, speed
    }
    transform.position.z = -0.5;
}

function moveTo (posY : float, speed : float)
{
    while (transform.position.y != posY)
    {
        transform.position = Vector2.MoveTowards (transform.position, new Vector2(posY), speed * Time.deltaTime);
        yield;
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题是在while循环中创建新的Vector2()时。 Vector2需要2个参数。

如果您不想修改X轴,请尝试以下操作:

while (transform.position.y != posY)
{
 transform.position = Vector2.MoveTowards (transform.position, new Vector2(transform.position.X, posY), speed * Time.deltaTime);
yield;

http://docs.unity3d.com/ScriptReference/Vector2.html