2016-12-06 61 views
2

我有一個腳本到目前爲止,只需點擊一下鼠標就可以移動一個小對象,但是我想改變它以便當我點擊這個對象,它會在另一個對象旁邊交換位置,而不是它現在移動的小距離。我對如何做到這一點感到困惑,因爲我對團結是新的。我如何獲得一個對象來移動並與另一個對象交換位置,鼠標clic

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); 
     } 


    } 
} 

回答

4

試試這個:

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變量,那麼你移動對象A到儲蓄銀行和對象B到POSA。

- 或 -

現在,如果對象B始終是一個不同的對象(不是常數),你不知道如何找到最近的對象,你可以使用光線投射。將以下函數添加到您的代碼:

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; 
    } 
} 

最後,變化更新到:

 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; 
      } 
     } 
    } 
3
using UnityEngine; 
using System.Collections; 

public class NewBehaviourScript : MonoBehaviour 
{ 
    //making them public just to be able watch values change in game mode 
    public float movementSpeed = 10; 
    public GameObject g1; 
    public GameObject g2; 
    public Vector3 vec1; 
    public Vector3 vec2 = new Vector3(2F, 2F, 2F); 
    public bool swapBack = false; 

    void Start() 
    { 
     g1 = GameObject.Find("Cube"); 
     g2 = GameObject.Find("Sphere"); 
     vec1 = new Vector3(g1.gameObject.transform.position.x, g1.gameObject.transform.position.y, g1.gameObject.transform.position.z); 
     vec2 = new Vector3(g2.gameObject.transform.position.x, g2.gameObject.transform.position.y, g2.gameObject.transform.position.z); 
    } 

    void Update() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      transform.Translate(Vector3.right * movementSpeed * Time.deltaTime); 
      swap(swapBack); 
     } 
    } 

    public void swap(bool back) 
    { 
     if (back) 
     { 
      g1.transform.position = vec1; 
      g2.transform.position = vec2; 
      swapBack = false; 
     } 
     else 
     { 
      g1.transform.position = vec2; 
      g2.transform.position = vec1; 
      swapBack = true; 
     } 
    } 
} 
相關問題