2016-12-06 7 views
2

마우스를 움직일 때 개체를 작은 거리만큼 움직이는 스크립트가 있지만 그 개체를 변경하려고합니다. 이 객체를 클릭하면 현재 움직이고있는 작은 거리가 아니라 다른 객체 옆에있는 장소와 바뀝니다. 나는이 일을하는 방법에 대해 약간 혼란 스럽다. 왜냐하면 나는 화합에 처음이기 때문에.개체를 이동하고 다른 개체와 장소를 바꿀 때 어떻게합니까?

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 변수로 각 객체의 위치를 ​​저장, 당신은 POSA에 POSB 및 objectB에 objectA 이동합니다. objectB가 (상수 NOT) 다른 객체가 항상 당신이 가장 가까운 개체를 찾을 방법을 잘하지 않은 경우

- 또는

지금, 당신은 레이 캐스트를 사용할 수 있습니다. 코드에 다음과 같은 기능을 추가

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