简体   繁体   English

Unity3d沿平面移动对象(仅限X和Z轴)

[英]Unity3d move object along plane (X and Z axis only)

I have a Unity3D project. 我有一个Unity3D项目。 This project has a prefab I created (a table top) with a plane as the top of the able. 这个项目有一个我创建的预制件(桌面),平面作为能量的顶部。 The plane has a box colider and rigid body. 飞机有一个箱子colider和刚体。 On the table top, I have another object. 在桌面上,我有另一个对象。 I want to move this object only around the table top (X and Z axis). 我想仅在桌面(X和Z轴)周围移动此对象。 The object should never be raised above the table top. 永远不要将物体抬高到桌面上方。 I have this code attached to my object: 我将此代码附加到我的对象:

void OnMouseDown()
{
    Debug.Log("mouse down");
    screenPoint = Camera.main.WorldToScreenPoint(transform.position);
    offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}

void OnMouseDrag()
{
    Debug.Log("mouse drag");
    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    transform.position = curPosition;
}

This moves the object around but it also moves the object above the table top. 这会移动对象,但它也会将对象移动到桌面上方。 Is there a way to keep the object on the table? 有没有办法将对象保持在桌面上?

It sounds like what you're looking for is to project the mousePosition onto the table. 听起来你正在寻找的是将mousePosition投影到表格上。 The mousePosition is originally in the screen space (which it looks like you've recognised with ScreenToWorldPoint), and you need to find the location of the table it hits from the screen position. mousePosition最初位于屏幕空间(看起来您已经使用ScreenToWorldPoint识别),并且您需要找到它从屏幕位置点击的表的位置。

Check out Physics.Raycast 查看Physics.Raycast

Once you have that point, you can just set the transform of the object to that, though you may want to set the vertical component to a constant value for that object rather than the resulting value. 获得该点之后,您可以将对象的变换设置为该变换,但您可能希望将垂直分量设置为该对象的常量值而不是结果值。

To do this, you may need to set the table to a collision layer (if you haven't already) and make sure you're looking for that only. 为此,您可能需要将表设置为碰撞层(如果尚未设置)并确保仅查找该表。 Note that with Raycast, the layerMask is a register, represented by an int (ie [0 1 2..31]), so you use bitwise operations to select your layers eg 请注意,对于Raycast,layerMask是一个寄存器,由int(即[0 1 2..31])表示,因此您使用按位运算来选择您的图层,例如

layerMask = (1<<LayerMask.NameToLayer("Table"))

Hope this helps! 希望这可以帮助!

You could utilize the already existing Unity code for mouse down. 您可以利用现有的Unity代码进行鼠标按下。 You can make those methods into when you have clicked and not clicked the mouse button which may be a simpler route. 您可以在单击时进行这些方法,而不是单击鼠标按钮,这可能是一个更简单的路径。

In terms of ensuring that it is only in the x and Z axes, if you don't want to worry about the collision aspect (which if the table is a rigidbody the mouse object should not fall through), you can always freeze the Y position in the Inspector so it locks to whatever level you set it at. 在确保它只在x和Z轴方面,如果你不想担心碰撞方面(如果表是一个刚体,鼠标对象不应该通过),你可以随时冻结Y在Inspector中的位置,以便锁定到您设置的任何级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM