移动实例化的游戏对象 [英] Moving a instantiated gameobject

查看:79
本文介绍了移动实例化的游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改instantiated game object的位置.为此,我使用了UI button,当用户单击按钮时,多维数据集将为instantiated,当用户单击该实例化的多维数据集并移动UI slider时,该多维数据集的位置将根据值进行更改由滑块给出.

I want to change the position of a instantiated game object. For that I have used a UI button when the user clicks on the button a cube will be instantiated and when user clicks on that instantiated cube and move a UI slider, the position of that cube will be changed according to the value given by the slider.

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

I tried this way but it doesn't work. What am I doing wrong 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;
    }
}

推荐答案

您应该具有一个回调函数,该函数在单击Button时将被调用,而在Slider值更改时将被调用.我无法确定您是否正在编辑器中执行此操作,但无法确定函数的命名方式,我们无法确定哪个是在Button单击或Slider值更改期间被调用的函数.

You are supposed to have a callback function that gets called when the Button is clicked and another one that gets called when the Slider value changes. I can't tell if you are doing this from the Editor but the way your functions are named, we can't tell which is one being called during Button click or Slider value change...

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

Put your Instantiate code in your Button callback function then put your cube moving code in the Slider value change callback function.

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

In your Raycast code that detects the cube click, store the Transform reference of the cube to a global Transform variable. This stored Transform is what you will use to move the cube in your Slider value change callback function.

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

You subscribe to Button click event with Button.onClick.AddListener(instantiateButtonCallBackFunction); then to Slider value change event with Slider.onValueChanged.AddListener(delegate { sliderCallBackFunction(cubeSlider.value); });

这是它的外观.一切都由代码完成.只需将Cube预制件SliderButton拖到正确的插槽中即可.单击Button时,将实例化多维数据集.单击多维数据集后,您将可以使用滑块移动它.

Here is what it should look like. Everything is done with code. Simply drag the Cube prefab, Slider and Button to the right slot and it should work. When a Button is clicked, Cube is instantiated. When the cube is clicked, you will be able to move it with the slider.

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

这篇关于移动实例化的游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆