移动实例化的游戏对象

Moving a instantiated gameobject

我想改变instantiated game object的位置。为此,当用户单击按钮时,我使用了 UI button,立方体将是 instantiated,当用户单击该实例化的立方体并移动 UI slider 时,该立方体的位置将根据滑块给出的值进行更改。

我试过这种方法,但没有用。我在这里做错了什么

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

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

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

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

myCube.transform.position.x = 10;

您应该有一个回调函数,当 Button 被点击时调用,另一个回调函数在 Slider 值改变时调用。我无法判断您是否在编辑器中执行此操作,但根据您的函数命名方式,我们无法判断在 Button 单击或 Slider 值更改期间调用的函数...

将您的 Instantiate 代码放入 Button 回调函数中,然后将您的方块移动代码放入 Slider 值更改回调函数中。

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

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

订阅了滑块值更改事件

它应该是这样的。一切都是用代码完成的。只需将立方体预制件 SliderButton 拖到正确的位置,它就可以工作了。单击 Button 时,将实例化 Cube。单击立方体时,您将能够使用滑块移动它。

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