简体   繁体   English

如何使Unity3d在表上移动对象?

[英]How to make Unity3d move objects on a table?

I am working on a tabletop game that user controls using accelerometer in the phone. 我正在研究一款桌面游戏,用户可以使用手机中的加速度计进行控制。 It is pretty similar to Labyrinth on iOS. 它与iOS上的迷宫非常相似。

The scene contains a tabletop created using planes and cubes, and a sphere as a ball. 该场景包含一个使用平面和立方体创建的桌面以及一个球形的球。 As it works in Labyrinth game, on tilting phone ball should drop to the tilted side, while the camera stays centered to table. 在迷宫游戏中,倾斜的手机球应下降到倾斜的一侧,而相机保持在桌子的中心。 I am trying to do similar thing where user tilts the phone, and objects on table move to tilted side. 我正在尝试做类似的事情,用户将手机倾斜,桌子上的物体移到倾斜的一侧。

Currently, I add the x and z component to Physics.gravity on tilt. 当前,我在倾斜时将x和z分量添加到Physics.gravity中。 Sadly, this change in gravity does not affect the ball which stays put on the table. 可悲的是,这种重力变化不会影响停留在桌子上的球。 Use gravity is selected for the ball, and it drop down from height to the tabletop initially and then comes to halt. 为球选择了使用重力,它最初从高度下降到桌面,然后停止。 After the initial drop, ball does not react to any gravity change. 初始下降后,球对重力变化没有反应。

I have also tried rotating the whole table, but using transform.rotate does not work either. 我也尝试过旋转整个表,但是使用transform.rotate也不起作用。 The table rotates perfectly, alongwith the camera, but the ball stays put hanging in the air. 桌子和相机一起旋转得很完美,但是球却一直悬在空中。 So, currently I am out of my depth about the issue. 因此,目前我对此问题不甚了解。

Is there any other way to allow tilt action registered, so that ball moves to the tilted direction? 还有其他方法可以记录倾斜动作,使球向倾斜方向移动吗? I cannot use addforce function, as there are multiple objects which need to react to tilt, and it will be difficult to keep track of that many addforce calls. 我不能使用addforce函数,因为有多个对象需要对倾斜做出反应,并且很难跟踪许多addforce调用。 How else can I get it working? 我还能如何使其正常工作?

See this post for some related information. 请参阅此帖子以获取一些相关信息。

As for why your sphere is sticking to the table, can I assume your sphere has a rigidbody? 至于为什么您的球体会粘在桌子上,我可以假设您的球体具有刚体吗? If so, you need need to wake it up: 如果是这样,则需要将其唤醒:

if (rigidbody.IsSleeping())rigidbody.WakeUp(); 如果(rigidbody.IsSleeping())rigidbody.WakeUp();

Ideally you would make that call only after detecting a change in orientation/gravity, not every frame. 理想情况下,仅在检测到方向/重力变化后才进行该调用,而不是每帧都进行。

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

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