您好,我希望能够点击某个对象,然后点击某个平面上的某个位置,并希望我的对象能够翻到该鼠标点击的位置。问题是这样做不止一个对象变得棘手。有人有任何想法吗?到目前为止,我已经在Unity3D的网站上按照高级脚本教程下的Coroutines教程进行了操作。这是代码:
附加到游戏对象:
private Vector3 target;
public float smoothing = 7f;
public Vector3 Target
{
get{return target;}
set
{
target = value;
StopCoroutine("Movement");
StartCoroutine("Movement",target);
}
}
IEnumerator Movement(Vector3 target)
{
while (Vector3.Distance(transform.position,target) > 0.05f)
{
transform.position = Vector3.Lerp(transform.position, target, smoothing* Time.deltaTime);
yield return null;
}
}
附在飞机上:
public propertiesandcoroutines coroutinescript;
private float Deltapos = 0.5f;
private GameObject collobj;
public void OnMouseDown()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Physics.Raycast (ray, out hit);
if (hit.collider.gameObject == gameObject)
{
coroutinescript.Target = new Vector3( hit.point.x,hit.point.y + Deltapos,hit.point.z);
}
}
此代码适用于一个游戏对象。如何将其更改为我点击的游戏对象?
答案 0 :(得分:0)
您只需要为每个脚本添加几行以获得所需的结果。
附加到可移动GameObjects的脚本
//This is the same type as the script attached to your plane. Rename it
//to whatever it's actually called (I've just called it PlaneScript). Also
//drag the plane into this field for each of the moveable GameObjects in the inspector
public PlaneScript planeScript;
//Paste this method anywhere inside the script attached to the GameObjects
void OnMouseUpAsButton () {
planeScript.SetObjectToMove(this);
}
现在,在附加到您的飞机的脚本中,添加以下内容
public void SetObjectToMove (propertiesandcoroutines script) {
coroutinescript = script;
}
工作原理
基本上,我们在这里做的是当你点击一个游戏对象时,我们在附加到飞机的脚本中重新分配“coroutinescript”变量。
因此,单击平面时,将移动在单击平面之前单击的最后一个GameObject。