2016-08-26 6 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という変数に格納します。これは、Slider値変更コールバック関数でキューブを移動するために使用するものですTransformが格納されます。あなたがここ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; 
関連する問題