2016-08-26 117 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值更改時。我不能告訴你,如果是從編輯器,但你的函數的命名方式這樣做,我們無法分辨這是一個過程Button點擊或Slider值的變化被稱爲...

把你Instantiate代碼你的Button回調函數,然後把你的多維數據集移動代碼在Slider值更改回調函數。

在您檢測立方體點擊的Raycast代碼中,將該立方體的Transform引用存儲到全局變量Transform。這個存儲Transform是你將用於移動你的Slider值更改回調函數中的立方體。

您訂閱Button click事件與Button.onClick.AddListener(instantiateButtonCallBackFunction);然後滑塊值更改事件與Slider.onValueChanged.AddListener(delegate { sliderCallBackFunction(cubeSlider.value); });

這是它應該是什麼樣子。一切都是用代碼完成的。只需將立方體預製件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; 
相關問題