繁体   English   中英

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

[英]Unity3D - Detecting Collisions on a Moving Platform

我正在尝试创建一个移动平台,该平台可以移动与其连接的对象,但也允许连接的对象检测高速碰撞。

我首先尝试使用transform.Translate()来移动对象,但这不支持那些高速碰撞。

注:我连接到平台的所有脚本示例都红色立方体下。

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;
}

物体穿墙


然后我尝试使用Rigidbody.MoveTowards(destination); 相反,但这产生了相同的结果:

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;
}

这是我在红色立方体上的刚体设置:

红立方体的组成部分

墙壁和平台都有一个标准的盒子对撞机。

我读过要检测这些高速碰撞,连续碰撞检测必须处于活动状态并且必须使用力来完成移动。 Unity 文档高速碰撞视频

力的问题是我不知道如何移动连接的 object,就像在以前的脚本中使用翻译所做的那样。

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

使用固定关节将不允许连接的 object 独立移动。


长话短说:

我如何创建一个移动平台,让上面的东西可以随之移动,同时还能够独立移动并高速检测碰撞?

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;
    } 
}

这段代码在 Platform.Platform 上,并且有一个额外的 Collider,它是一个 Trigger。 当玩家在触发区域内时,他的速度会被直接修改,并且他可以在仍然是动态刚体的情况下改变速度。 平台还对旋转和垂直轴有限制。

翻译允许“传送”穿过你观察到的墙壁。 如果你足够快,或者 FPS 足够低,它就会穿过墙。

选项:

  1. 使用Raycast保护翻译。 从旧到新 position。
  2. 使用SpherecastBoxcast保护翻译
  3. 将平台速度 + 玩家输入(这样你仍然可以走路)应用到玩家身上。

还要检查碰撞检测设置:

在此处输入图像描述

但请注意,使用鼠标在场景中移动 object 不会设置刚体的速度。 因此,要测试它是否没有穿过墙壁,您需要使用物理学来乒乓球您的平台。

暂无
暂无

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

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