C# 如何在Unity 3D中将对象移动到鼠标的z和y坐标
有什么帮助吗?这是为了一场比赛,我还有48个小时要完成。谢谢 给你:C# 如何在Unity 3D中将对象移动到鼠标的z和y坐标,c#,unity3d,C#,Unity3d,有什么帮助吗?这是为了一场比赛,我还有48个小时要完成。谢谢 给你: private void Update() { // Get the mouse position Vector3 mousePos = Input.mousePosition; // Set the z component of the mouse position to the absolute distance between the camera's z and the object's z.
private void Update()
{
// Get the mouse position
Vector3 mousePos = Input.mousePosition;
// Set the z component of the mouse position to the absolute distance between the camera's z and the object's z.
mousePos.z = Mathf.Abs(Camera.main.transform.position.z - transform.position.z);
// Determine the world position of the mouse pointer
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
// Update the position of the object
transform.position = worldPos;
}
确切地说是什么
transform.position=新矢量3(transform.position.x,worldPos.y,worldPos.z)代码>尝试做什么?尝试vector3worldpos=Camera.main.ScreenToWorldPoint(新的Vector3(mousePos.x,mousePos.y,transform.position.z))代码>而不是相机的近Clipplane。@HumanWrites它不工作。当我把鼠标移出屏幕中央的一小部分时,worldPos会发疯(在x坐标上是1万亿以上),它一开始不会移动到我的鼠标上,而是移动到另一侧@Putnam它将橙色框(脚本附在其上)移动到worldPos变量的y和z位置(应该是鼠标位置),并保持其x坐标不变。
private void Update()
{
// Get the mouse position
Vector3 mousePos = Input.mousePosition;
// Set the z component of the mouse position to the absolute distance between the camera's z and the object's z.
mousePos.z = Mathf.Abs(Camera.main.transform.position.z - transform.position.z);
// Determine the world position of the mouse pointer
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
// Update the position of the object
transform.position = worldPos;
}