Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 立方体平移_C#_Unity3d - Fatal编程技术网

C# 立方体平移

C# 立方体平移,c#,unity3d,C#,Unity3d,我不明白为什么我的立方体不能沿Z方向在fineMovimento(endMovement)矢量3位置进行平移 下面是脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class MovimentoPorta : MonoBehaviour { public Transform target; Vector3 fineMovimento = new

我不明白为什么我的立方体不能沿Z方向在fineMovimento(endMovement)矢量3位置进行平移

下面是脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovimentoPorta : MonoBehaviour {

    public Transform target;
    Vector3 fineMovimento = new Vector3(221.04f, -8.98f,329);
    void Update () {
        if (Input.GetKeyDown(KeyCode.P))
        {
            while (target.position != fineMovimento)
            {
                target.Translate (Vector3.forward*10.0f*Time.deltaTime); //il cubo si doverbbe muovere lungo l'asse Z +
            }
        }
    }
}
不要在
void Update()
中使用
while(…)
进行翻译(因为在播放模式下按p键时Unity会挂起)。如果希望平滑地移动到
fineMovimento
,一种方法是使用
Vector3.Lerp()

试试这个:

public Transform target;
Vector3 fineMovimento;
float smoothTime = 0.125f;

void Start()
{
    fineMovimento = target.position; // Initialize
}

void Update () 
{
    target.position = Vector3.Lerp(target.position, fineMovimento, smoothTime);

    if (Input.GetKeyDown(KeyCode.P))
    {
        fineMovimento = new Vector3(221.04f, -8.98f,329); // Set end position
    }
}

希望这是你想要的。

真的谢谢你,伙计。。。这正是我想要的。。。游戏打得好