Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 移动实例化的游戏对象_C#_Unity3d_Raycasting - Fatal编程技术网

C# 移动实例化的游戏对象

C# 移动实例化的游戏对象,c#,unity3d,raycasting,C#,Unity3d,Raycasting,我想更改实例化游戏对象的位置。为此,我使用了一个UI按钮,当用户单击按钮时,多维数据集将被实例化,当用户单击该实例化的多维数据集并移动UI滑块,该多维数据集的位置将根据滑块给出的值进行更改 using UnityEngine; using System.Collections; using UnityEngine.UI; public class instantiate : MonoBehaviour { public GameObject cubePrefab; public

我想更改
实例化游戏对象的位置
。为此,我使用了一个
UI按钮
,当用户单击按钮时,多维数据集将被
实例化
,当用户单击该实例化的多维数据集并移动
UI滑块
,该多维数据集的位置将根据滑块给出的值进行更改

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

我试过这种方法,但不起作用。我做错了什么

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;

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

GameObject myCube = Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity);
然后,您可以使用该引用控制其位置

myCube.transform.position.x = 10;

您应该有一个回调函数,在单击
按钮时调用该函数,另一个回调函数在
滑块
值更改时调用。我无法判断您是否在编辑器中执行此操作,但根据您的函数命名方式,我们无法判断在
按钮
单击或
滑块
值更改过程中调用的是哪个函数

实例化
代码放入
按钮
回调函数,然后将多维数据集移动代码放入
滑块
值更改回调函数

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

使用
按钮.onClick.AddListener(实例化按钮调用函数)订阅
按钮
单击事件然后使用
Slider.onValueChanged.AddListener(委托{sliderCallBackFunction(cubeSlider.value);})更改滑块值事件

这是它应该是什么样子。一切都是用代码完成的。只需将多维数据集预置、
滑块
按钮
拖动到右侧插槽,它就可以工作了。单击
按钮
时,将实例化多维数据集。单击立方体时,您将能够使用滑块移动它

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

您应该有一个回调函数,在单击
按钮时调用该函数,另一个回调函数在
滑块
值更改时调用。我无法判断您是否在编辑器中执行此操作,但根据您的函数命名方式,我们无法判断在
按钮
单击或
滑块
值更改过程中调用的是哪个函数

实例化
代码放入
按钮
回调函数,然后将多维数据集移动代码放入
滑块
值更改回调函数

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

使用
按钮.onClick.AddListener(实例化按钮调用函数)订阅
按钮
单击事件然后使用
Slider.onValueChanged.AddListener(委托{sliderCallBackFunction(cubeSlider.value);})更改滑块值事件

这是它应该是什么样子。一切都是用代码完成的。只需将多维数据集预置、
滑块
按钮
拖动到右侧插槽,它就可以工作了。单击
按钮
时,将实例化多维数据集。单击立方体时,您将能够使用滑块移动它

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

滑动对象时,您想移动哪个轴?@Programmer Z axis滑动对象时,您想移动哪个轴?@Programmer Z axis谢谢,我理解得很好谢谢,我理解得很好