如何在鼠标键盘上使一个对象移动并与另一个对象交换位置 [英] How do I get an object to move and swap places with another object, on a mouse clic
问题描述
到目前为止,我有一个脚本,可以通过单击鼠标将对象移动一小段距离,但是我想对其进行更改,以便当我单击该对象时,它会与旁边的另一个对象交换位置,而不仅仅是很小的对象它正在移动的距离.我对如何做到这一点有些困惑,因为我是团结的新手.
I have a script so far that moves an object a small distance upon a mouse click, however I want to change it so that when I click this object, it swaps places with another obejct next to it, instead of just the small distance it is moving now. I am a little confused on how to do this, because I am new to unity.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript: MonoBehaviour
{
public float movementSpeed = 10;
void Update(){
if ( Input.GetMouseButtonDown(0))
{
transform.Translate(Vector3.right * movementSpeed * Time.deltaTime);
}
}
}
推荐答案
尝试一下:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript: MonoBehaviour {
public GameObject objectA; //Needs to be initialized in the editor, or on Start
public GameObject objectB; //Needs to be initialized in the editor, or on Start
public float movementSpeed = 10;
private Vector3 posA = Vector3.zero; //Vector3.zero is for initialization
private Vector3 posB = Vector3.zero; //Vector3.zero is for initialization
void Update() {
if ( Input.GetMouseButtonDown(0)) {
posA = objectA.gameObject.transform.position;
posB = objectB.gameObject.transform.position;
objectA.gameObject.transform.position = posB;
objectB.gameObject.transform.position = posA;
}
}
}
这只是将每个对象的位置保存到posA和posB变量中,然后将objectA移到posB,将objectB移到posA.
This just saves each objects position into the posA and posB variables, then you move objectA to posB and objectB to posA.
-或-
现在,如果objectB始终是一个不同的对象(不是常量),并且您不确定如何找到最近的对象,则可以使用射线投射.在代码中添加以下功能:
Now if objectB is always a different object (NOT constant) and you aren't sure how to find the nearest object, you could use a raycast. Add the following function to your code:
gamObject NearestObject () {
int dist;
int nearestIndex;
//Create an array to contain objects to be hit by the raycast
RaycastHit[] nearby;
//Hit all objects within 100 units with a raycast, change the 100 as needed
nearby = Physics.RaycastAll(objectA.transform.position, transform.forward, 100.0f);
//Check if there is at least one object
if(nearby.Length > 0) {
//If there is only one object and it's not objectA
if(!(nearby.Length == 1 && nearby[0].transform == objectA.transform)) {
dist = nearby[0].distance;
nearestIndex = 0;
for (int i = 1; i < nearby.Length; i++) {
if(nearby[i].transform != gameObject.transform && nearby[i].distance < dist)
dist = nearby[i].distance;
nearestIndex = i;
}
}
} else {
//There is only one object in the raycast and it is objectA
nearestIndex = -1;
}
} else {
//There are no objects nearby
nearestIndex = -1;
}
//nearestIndex will only be negative one if there are no objects near objectA, so return null
if (nearestIndex == -1) {
return null;
} else {
//return nearest object to update
return nearby[nearestIndex].gameObject;
}
}
最后,将更新"更改为:
Finally, change Update to:
void Update() {
if ( Input.GetMouseButtonDown(0)) {
objectB = NearestObject ();
if (objectB != null) {
posA = objectA.gameObject.transform.position;
posB = objectB.gameObject.transform.position;
objectA.gameObject.transform.position = posB;
objectB.gameObject.transform.position = posA;
}
}
}
这篇关于如何在鼠标键盘上使一个对象移动并与另一个对象交换位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!