简体   繁体   English

Unity3D - 检测移动平台上的碰撞

[英]Unity3D - Detecting Collisions on a Moving Platform

I am trying to create a moving platform that moves what is connected to it but also allow the objects that are connected to detect high-speed collisions.我正在尝试创建一个移动平台,该平台可以移动与其连接的对象,但也允许连接的对象检测高速碰撞。

I have first tried to use transform.Translate() to move the objects but this doesn't support those high-speed collisions.我首先尝试使用transform.Translate()来移动对象,但这不支持那些高速碰撞。

Note: All script examples I have connected to the platform which is under the red cube.注:我连接到平台的所有脚本示例都红色立方体下。

public Transform connectedTo; // The red cube in the gif
private Vector3 lastPosition;

void FixedUpdate() {
    // Calculate how much the vector has changed
    Vector3 amountChanged = transform.position - lastPosition;

    // Apply the amount changed to the connected object
    connectedTo.transform.position += amountChanged;

    // Update the last position
    lastPosition = transform.position;
}

物体穿墙


I then tried to use Rigidbody.MoveTowards(destination);然后我尝试使用Rigidbody.MoveTowards(destination); instead but that was producing the same results:相反,但这产生了相同的结果:

public Transform connectedTo; // The red cube in the gif
private Vector3 lastPosition;

void FixedUpdate() {
    // Calculate how much the vector has changed
    Vector3 amountChanged = transform.position - lastPosition;

    // Get the point in which the object must move to
    Vector3 destination = connectedTo.transform.position + amountChanged;

    // Apply the amount changed to the connected object
    connectedTo.GetComponent<Rigidbody>().MoveTowards(destination);

    // Update the last position
    lastPosition = transform.position;
}

This is my Rigidbody setup on my red cube:这是我在红色立方体上的刚体设置:

红立方体的组成部分

The wall and platform both have a standard box collider.墙壁和平台都有一个标准的盒子对撞机。

I have read that to detect these high-speed collisions Continuous collision detection must be active AND the movement must be done using forces.我读过要检测这些高速碰撞,连续碰撞检测必须处于活动状态并且必须使用力来完成移动。 Unity Documentation & High Speed Collision Video Unity 文档高速碰撞视频

The issue with forces is I don't know how to move the connected object as was done in the previous scripts using translation.力的问题是我不知道如何移动连接的 object,就像在以前的脚本中使用翻译所做的那样。

// This barely moves the object connected to the platform
connectedTo.GetComponent<Rigidbody>().AddForce(amountChanged, ForceMode.Impulse); 

Using a fixed joint will not allow the connected object to move independently.使用固定关节将不允许连接的 object 独立移动。


TL;DR:长话短说:

How do I create a moving platform that will allow things on top to move with it while also being able to move independently AND detect collisions at high speeds?我如何创建一个移动平台,让上面的东西可以随之移动,同时还能够独立移动并高速检测碰撞?

public class movement : MonoBehaviour
{
    private Rigidbody rb;
    
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody>();
    }
    
    void Update()
    {
        rb.velocity = new Vector3(-25,0,0);
    }
    
    private void OnTriggerStay(Collider other)
    {
        other.GetComponent<Rigidbody>().velocity = rb.velocity;
    } 
}

This code is on Platform.Platform and has extra Collider that is a Trigger.这段代码在 Platform.Platform 上,并且有一个额外的 Collider,它是一个 Trigger。 when player is inside trigger area his velocity is directly modified and he can ave its speed altered while stil being dynamic rigidbody.当玩家在触发区域内时,他的速度会被直接修改,并且他可以在仍然是动态刚体的情况下改变速度。 Platform also has constraints on rotations and vertical axis.平台还对旋转和垂直轴有限制。

Translation allows "teleporting" though the wall as you observed.翻译允许“传送”穿过你观察到的墙壁。 If you are fast enough, or FPS are low enough, it will pass though the wall.如果你足够快,或者 FPS 足够低,它就会穿过墙。

Options:选项:

  1. Secure the translation with a Raycast .使用Raycast保护翻译。 From old to new position.从旧到新 position。
  2. Secure the translation with a Spherecast or Boxcast使用SpherecastBoxcast保护翻译
  3. Apply the velocity of the platform + playerinput (so you can still walk) to the player.将平台速度 + 玩家输入(这样你仍然可以走路)应用到玩家身上。

Also check out the Collision Detection settings:还要检查碰撞检测设置:

在此处输入图像描述

But please note, that moving the object in scene using your mouse will not set the velocity of the rigidbody.但请注意,使用鼠标在场景中移动 object 不会设置刚体的速度。 So to test if it's not passing through the wall, you need to ping-pong your platform using physics.因此,要测试它是否没有穿过墙壁,您需要使用物理学来乒乓球您的平台。

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

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