Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
User interface 如何将游戏对象转换到所需位置并返回到起始位置?_User Interface_Unity3d_Position_Transform_Game Physics - Fatal编程技术网

User interface 如何将游戏对象转换到所需位置并返回到起始位置?

User interface 如何将游戏对象转换到所需位置并返回到起始位置?,user-interface,unity3d,position,transform,game-physics,User Interface,Unity3d,Position,Transform,Game Physics,我是UnityScript的新手,我有以下代码可以在“Z”轴上向前移动游戏对象,但需要一些改进。让我解释一下 单击GUI.按钮时,脚本将运行。游戏对象开始移动…直到无限远。 我试图让它移动到所需的位置(例如,一个空的游戏对象位置),但根本不起作用 在第一个GUI上,我应该如何完善这个代码片段,将游戏对象移动到所需的位置。单击按钮,然后在单击后返回到起始位置 此外,是否可以在同一个GUI上一步一步地进行此移动 以下是代码片段: #pragma strict // member variables

我是UnityScript的新手,我有以下代码可以在“Z”轴上向前移动游戏对象,但需要一些改进。让我解释一下

单击GUI.按钮时,脚本将运行。游戏对象开始移动…直到无限远。 我试图让它移动到所需的位置(例如,一个空的游戏对象位置),但根本不起作用

在第一个GUI上,我应该如何完善这个代码片段,将游戏对象移动到所需的位置。单击按钮,然后在单击后返回到起始位置

此外,是否可以在同一个GUI上一步一步地进行此移动

以下是代码片段:

#pragma strict

// member variables (declared outside any function)
var startPos: Vector3;
var endPos : Vector3;
var cachedTransform : Transform;
private static var isforward = false;

// save pos before moving:
startPos = transform.position;

// make the gameObject transform, then restore the initial position when needed:
transform.position = startPos;

function Awake() {
startPos = transform.localPosition;
}

function Start() {
cachedTransform = transform;
startPos = cachedTransform.position;
}

function FixedUpdate() {

if(isforward){

var translation : float;

if (cachedTransform.position.x == endPos)
 {
  cachedTransform.position = startPos;
 }
 else
 {
    translation = Time.deltaTime * 2;
    cachedTransform.Translate(0, 0, translation);
    cachedTransform.Translate(Vector3.forward * translation);
 }
}
}
 static function doforward ()
{
    isforward = !isforward;
}

提前感谢大家的回答。

如果在游戏对象上附加脚本,则可以轻松地移动它,然后(在JavaScript中)


然后,当需要移动对象时,只需调用这些函数。

您可能需要查看
Vector3.Lerp
方法。这将两个向量(点)和一个浮点作为参数,然后返回一个点,这是它们之间的一小部分。例如,
Lerp(从,到,0.3f)
将为您提供两点之间30%的路径。一旦你有了这个,你需要做的就是设置对象的变换。

Hello@user3183542。谢谢你对我问题的回答。我已将您发布的脚本附加到游戏对象,并使用GUI.按钮使其正常运行,但什么也没有发生。我是不是遗漏了什么?此外,在我的原始代码片段中,如何将gameObject移回原始位置?除非调用脚本中的函数,否则脚本不会执行任何操作。查看您的脚本,我不确定(isforward)变量是什么,以及它来自哪里。看起来endPos只是一个空向量3。另外,cachedTransform.position.x==endPos将永远不会返回true,因为endPos是一个向量3,所以您需要endPos.x(isforward)变量用作GUI.Button与另一个脚本交互。如何调用脚本中的函数?需要写更多的代码吗?你能提供一个示例代码吗?我会得到脚本并像这样调用函数GameObject.Find('NameOfGameObject').GetComponent(YourScriptName).moveToStart();你的按钮上有我的脚本,所以无论你从哪里控制动作,在脚本中使用我刚刚发布的内容来查找游戏对象及其脚本,然后调用其上的函数。
#pragma strict    

var startPosition;

// Record the starting position when the scene loads
function Start () {
startPosition = gameObject.transform.position;

}

// Call this to move you object to wherever
function moveObject () {

var newPos = new Vector3 (10,20,0); //(where ever you need it to go) 

gameObject.transform.position = newPos;   

}

// Call this to move the object to starting position, using variable we made at start
function moveToStart () {
gameObject.transform.position = startPosition;
}