移动实例化的游戏对象

时间:2016-08-26 00:43:15

标签: c# unity3d raycasting

我想改变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;
    }
}

2 个答案:

答案 0 :(得分:2)

你应该有一个回调函数,当Button被点击时被调用,而另一个函数在Slider值改变时被调用。我不知道你是从编辑器那里做的,而是你的函数的命名方式,我们无法分辨在Button点击或Slider值更改期间调用的是哪一个... < / p>

Instantiate代码放入Button回调函数中,然后将多维数据集移动代码放入Slider值更改回调函数中。

在检测到多维数据集点击的Raycast代码中,将多维数据集的Transform引用存储到全局Transform变量中。存储的Transform用于在Slider值更改回调函数中移动多维数据集。

您使用Button订阅了Button.onClick.AddListener(instantiateButtonCallBackFunction);点击事件,然后使用Slider.onValueChanged.AddListener(delegate { sliderCallBackFunction(cubeSlider.value); });订阅了Slider值更改事件

这应该是什么样子。一切都是用代码完成的。只需将立方体预制件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 :(得分:0)

您需要存储对您已创建的实例的引用。

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

然后,您可以使用该参考来控制它的位置。

myCube.transform.position.x = 10;