c# - 立方体翻译
问题描述
我不明白为什么我的立方体不会在沿 Z 的fineMovimento(endMovement)vector3 位置平移。
这里的脚本:
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 +
}
}
}
}
解决方案
不要使用while(...)
invoid Update()
进行翻译(因为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
}
}
希望这是你想要的。