简体   繁体   English

我如何让我的播放器在 unity 2d 中随着移动平台移动

[英]How do i make my player move with a moving platform in unity 2d

I'm making a 2D platformer and decided to add sticky platforms.我正在制作 2D 平台游戏并决定添加粘性平台。 I've made the platforms move, but the player doesn't move with them.我已经让平台移动了,但播放器并没有随之移动。

However, after parenting the player to the platform, the player still falls through.然而,在将玩家引导至平台后,玩家仍然失败。 I have added two BoxCollider2D s and set one of them as a Trigger .我添加了两个BoxCollider2D并将其中之一设置为Trigger None of the colliders have a RigidBody2D所有碰撞器都没有RigidBody2D

public class StickyPlatform : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Player")
            collision.gameObject.transform.SetParent(transform);
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Player")
            collision.gameObject.transform.SetParent(null);
    }
}

trigger does not stop object move through the box, you can try OnCollisionEnter.触发器不会阻止对象在框中移动,您可以尝试 OnCollisionEnter。 Usually I would have a ground check and you can say if(isGrounded&&onPlatform) //move with platform通常我会进行地面检查,你可以说if(isGrounded&&onPlatform) //move with platform

So it sounds you have two problems:所以听起来你有两个问题:

  1. Player is falling through platform玩家正在通过平台坠落
  2. Player isn't sticking to the platform玩家不依赖平台

Falling through the floor is a super common bug, with many causes.从地板上掉下来是一个非常常见的错误,有很多原因。 Here is a checklist I found:这是我找到的清单

  1. does Object have a Collider? Object 有 Collider 吗? If not, select Object如果不是,请选择对象
  2. go to the top bar去顶栏
  3. components组件
  4. physics物理
  5. choose appropriate collider (if Terrain, check the last tab, the little cog-wheel)选择合适的对撞机(如果是地形,请检查最后一个选项卡,小齿轮)

Note: mesh-collider may cause problems Particularly, if both FallingObject and GroundObject have mesh-collider Particularly, if the mesh is animated To avoid mesh-collider, you can build an aproximate shape of your mesh from several primitive colliders (in parent-, child- or sibling-GameObjects) If you need a Mesh-collider no matter what, you can try to place additional primitive colliders where they won't be in the way to 'enforce' the collisions注意:网格碰撞器可能会导致问题特别是,如果 FallingObject 和 GroundObject 都有网格碰撞器特别是,如果网格是动画的为了避免网格碰撞器,你可以从几个原始碰撞器(在父, child- or sibling-GameObjects) 如果无论如何你都需要一个 Mesh-collider,你可以尝试放置额外的 primitive colliders 在它们不会妨碍“执行”碰撞的地方

Is the Object a Trigger?对象是触发器吗? If so, select Object如果是这样,请选择对象

  1. find its Collider-Component (if Terrain, check the last tab, the little cog-wheel)找到它的 Collider-Component(如果是 Terrain,检查最后一个选项卡,小齿轮)
  2. remove the check of 'IsTrigger'删除“IsTrigger”的检查

Is the Collider placed well? Collider 放置得好吗? Fiddle with center, size and skin-width (start with 0.1) until the green outline aproximately fits the character (If you get really strange values, it might be due to scale (eg your mesh was way too big so you downsized to.01))调整中心、大小和皮肤宽度(从 0.1 开始)直到绿色轮廓近似适合角色(如果你得到非常奇怪的值,可能是由于比例(例如你的网格太大所以你缩小到.01) ))

You may try to zero out all positioning (both unity and your modeling-program)您可以尝试将所有定位归零(统一和您的建模程序)

The link goes into even more cases.该链接涉及更多案例。

Once the player can stay on, the physics engine should handle the momentum transfer to move the player with the platform using friction.一旦玩家可以继续前进,物理引擎应处理动量传递,以使用摩擦力使玩家与平台一起移动。 Which again, requires RigidBody2D .这又需要RigidBody2D I'm not sure why you're not using RigidBody s, it kind of feels like you're avoiding the solution.我不确定您为什么不使用RigidBody s,感觉就像您在避免解决方案。

Doing it this way should avoid the need to parent the player, and have a trigger volume as well, unless you want the player physically stuck and can't move on the platform.以这种方式进行应该避免需要父母的玩家,并且有一个触发音量,除非你想让玩家身体卡住并且不能在平台上移动。

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

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