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谢谢,我理解得很好谢谢,我理解得很好