2016-08-26 2 views
0

instantiated game object의 위치를 ​​변경하고 싶습니다. 사용자가 버튼을 클릭 할 때 UI button을 사용하면 입방체는 instantiated이고 사용자가 해당 인스턴스화 된 큐브를 클릭하고 UI slider을 움직이면 그 입방체의 위치가 슬라이더에 지정된 값에 따라 변경됩니다. 인스턴스화 된 게임 객체 이동

enter image description here

나는이 방법을 시도했지만 작동하지 않습니다. 내가 여기서 잘못하고있는 것

using UnityEngine; 
using System.Collections; 

public class instantiate : MonoBehaviour 
{ 
    public GameObject cube; 
    public float speed = 0f; 
    public float pos = 0f; 

    // Use this for initialization 
    void Start() 
    { 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      RaycastHit hit; 
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
      if (Physics.Raycast(ray, out hit, 100.0f)) 
      { 
       Debug.Log("Clicked"); 

       if (hit.collider.tag == "Cube") 
       { 

        // Destroy(hit.collider.gameObject); 

        // Destroy(this); 
        speed += Input.GetAxis("Horizontal"); 
        hit.collider.gameObject.transform.eulerAngles = new Vector3(0, 0, speed); 
        hit.collider.gameObject.transform.position = new Vector3(0, 0, pos);//pos 
       } 
      } 
     } 

    } 

    public void objinst() 
    { 
     Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity); 
    } 

    public void rotatess(float newspeed) 
    { 
     speed = newspeed; 

    } 

    public void positions(float newpos) 
    { 

     pos = newpos; 
    } 
} 
+0

개체를 슬라이드 할 때 어떤 축을 이동 하시겠습니까? – Programmer

+0

@ 프로그래머 Z 축 – user3789211

답변

2

은 당신이 Button을 클릭 할 때 호출되는 콜백 함수와 때 Slider 값 변경 호출되는 또 다른 하나가 가정된다. 나는

은 당신의 Instantiate 코드를 넣어 ... 우리가 Button 클릭 또는 Slider 값 변경시 호출되는 하나입니다 말할 수 없다, 당신은 편집기하지만 기능의 이름을 지정하는 방식에서이 작업을 수행하는 경우 말할 수 없다 Button 콜백 함수는 큐브 이동 코드를 Slider 값 변경 콜백 함수에 넣습니다.

큐브 클릭을 감지하는 Raycast 코드에서 큐브에 대한 Transform 참조를 변수 Transform에 저장합니다. 이 Transform을 저장하면 Slider 값 변경 콜백 함수에서 큐브를 이동하는 데 사용됩니다.

당신은 여기 Slider.onValueChanged.AddListener(delegate { sliderCallBackFunction(cubeSlider.value); });

가 같아야 무엇과 슬라이더 값 변경 이벤트에 다음 Button.onClick.AddListener(instantiateButtonCallBackFunction);Button 클릭 이벤트에 가입. 모든 것은 코드로 처리됩니다. 큐브 프리 패브릭 SliderButton을 오른쪽 슬롯으로 드래그하기 만하면 작동합니다. Button을 클릭하면 큐브가 인스턴스화됩니다. 큐브를 클릭하면 슬라이더로 이동할 수 있습니다.

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 

public class instantiate : MonoBehaviour 
{ 
    public GameObject cubePrefab; 
    public Slider cubeSlider; 
    public Button instantiateButton; 

    public float speed = 0f; 
    public float pos = 0f; 


    private Transform currentObjectToDrag = null; 

    // Use this for initialization 
    void Start() 
    { 
     //Set Slider Values 
     cubeSlider.minValue = 0f; 
     cubeSlider.maxValue = 50f; 
     cubeSlider.value = 0f; 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (Input.GetMouseButtonDown(0)) 
     { 
      RaycastHit hit; 
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
      if (Physics.Raycast(ray, out hit, 1000.0f)) 
      { 
       GameObject objHit = hit.collider.gameObject; 
       Debug.Log("We Clicked on : " + objHit.name); 

       //Check if this is cube 
       if (objHit.CompareTag("Cube")) 
       { 
        Debug.Log("Cube selected. You can now drag the Cube with the Slider!"); 
        //Change the current GameObject to drag 
        currentObjectToDrag = objHit.transform; 
       } 
      } 
     } 
    } 

    public void instantiateCube() 
    { 
     //Instantiate(cubePrefab, new Vector3(0, 0, 0), Quaternion.identity); 
     Instantiate(cubePrefab, new Vector3(-15.1281f, 0.67f, 7.978208f), Quaternion.identity); 
    } 

    public void rotatess(float newspeed) 
    { 
     speed = newspeed; 

    } 

    public void positions(float newpos) 
    { 
     pos = newpos; 
    } 

    //Called when Instantiate Button is clicked 
    void instantiateButtonCallBack() 
    { 
     Debug.Log("Instantiate Button Clicked!"); 
     instantiateCube(); 
    } 

    //Called when Slider value changes 
    void sliderCallBack(float value) 
    { 
     Debug.Log("Slider Value Moved : " + value); 

     //Move the Selected GameObject in the Z axis with value from Slider 
     if (currentObjectToDrag != null) 
     { 
      currentObjectToDrag.position = new Vector3(0, 0, value); 
      Debug.Log("Position changed!"); 
     } 
    } 

    //Subscribe to Button and Slider events 
    void OnEnable() 
    { 
     instantiateButton.onClick.AddListener(instantiateButtonCallBack); 
     cubeSlider.onValueChanged.AddListener(delegate { sliderCallBack(cubeSlider.value); }); 
    } 

    //Un-Subscribe to Button and Slider events 
    void OnDisable() 
    { 
     instantiateButton.onClick.RemoveListener(instantiateButtonCallBack); 
     cubeSlider.onValueChanged.RemoveListener(delegate { sliderCallBack(cubeSlider.value); }); 
    } 
} 
+1

고맙습니다. 아이디어를 잘 설명해 주셔서 감사합니다. – user3789211

0

작성한 인스턴스에 대한 참조를 저장해야합니다.

GameObject myCube = Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity); 

그런 다음 해당 참조를 사용하여 위치를 제어 할 수 있습니다.

myCube.transform.position.x = 10;