[英]Unity 3D. How do I move the object up when the another object gets on it
I have the object "ball" that can roll on the surface. 我有可以在表面上滚动的对象“球”。 There is also the object "platform".
还有一个对象“平台”。 So, I need to platform up a certain distance, only when the ball will stop on the platform.
因此,我只需要将球停在平台上一定距离即可。
Script on C#. C#上的脚本。
Honsetly - I don't know about Unity API anything :), but I know that it must have available property "position". 老实说-我对Unity API一无所知:),但是我知道它必须具有可用的属性“ position”。 So one of the ways to solve you problem - check position of ball in every Update(in comparison with previous position), and when it's position become stable(the same) for a while(some time) - move the platform.
因此,解决问题的一种方法是-在每次更新中检查球的位置(与以前的位置比较),并在一段时间(一段时间内)保持稳定(相同)时移动平台。
something like: 就像是:
private Vector3 lastPosition;
private stableTime = 0;
public float distThreshold = 0.1;
public float timeThreshold = 0.1;
void FixedUpdate() {
if (lastPoistion == null || Vector3.Distance(ball.position, lastPoistion) > distThreshold) {
lastPosition = ball.position;
stableTime = 0;
}
else
stableTime += Time.deltaTime;
if (stableTime > timeThreshold)
platform.MovePoistion()
}
You need to identify 2 things here. 您需要在此处识别2件事情。
To identify if the ball is stopped simply keep track of it's current speed (using a speed
property assigned to the ball object). 要确定球是否已停止,只需跟踪其当前速度(使用分配给球对象的
speed
属性)。 I'm assuming you probably already have this as the player will be controlling the ball? 我假设您可能已经拥有这个,因为球员将控制球? If so, just check that
speed == 0
. 如果是这样,只需检查
speed == 0
。
To check whether the ball is on the platform you will need to use colliders. 要检查球是否在平台上,您将需要使用对撞机。 Take a look under 2d or 3d physics (you haven't specified which you are using) and apply some appropriate colliders to both the platform and the ball.
看一下2d或3d物理(尚未指定正在使用的物理),然后将适当的对撞机应用于平台和球。 You can now programmatically check for collision between the 2 objects.
现在,您可以以编程方式检查2个对象之间的碰撞。 There are plenty of tutorials on object collision so I won't go into it here but this may be a good place to start.
有很多的物体碰撞的教程,所以我不会去到这里,但是这可能是一个良好的开端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.