简体   繁体   English

Unity 3D。 当另一个物体到达上面时,如何向上移动该物体

[英]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件事情。

  • Is the ball stopped? 球停了吗?
  • is the ball on the platform 是平台上的球

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.

相关问题 Unity 3D。 子对象不会更改 - Unity 3D. Child object is not changed Unity3D:如何相对于另一个对象移动一个对象? - Unity3D: How to move an object in relation to another object? 在 Unity 中触摸 3d object 时,如何使 int go 加一 - How do I make an int go up by one when a 3d object is touched in Unity 如何在unity3d中平稳地上下移动刚体对象 - How to move rigidbody object up or down smoothly in unity3d 使用向上和向下箭头键移动对象时,如何使另一个对象沿x轴移动? - How do I make another object follow across x - axis when I move an object using Up and Down arrow keys? 带有 Unity 3D 的 C#:当用户移动鼠标时,如何让相机围绕对象移动 - C# with Unity 3D: How do I make a camera move around an object when user moves mouse 当玩家 object 在一个统一的 3d 游戏中变得更大时,如何让相机移动 position? - How do I make the camera move position as the players object becomes bigger in a unity 3d game? Unity 5:如何使一个对象移动到另一个对象? - Unity 5: How to make an object move to another object? c# unity2d:如何移动 Object 并在碰撞时改变方向? - c# unity2d: How can I move an Object and to change the direction when it collides? 如何在 Unity (2D) 中使用鼠标移动 object? - How to move an object with the mouse in Unity (2D)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM