gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:32:52 28 4
gpt4 key购买 nike

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

我首先尝试使用 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;
}

Object passing through wall


然后我尝试使用 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;
}

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

The Red Cube's components

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

我读到,要检测这些高速碰撞,必须激活连续碰撞检测,并且必须使用力来完成移动。 Unity Documentation & High Speed Collision Video

力的问题是我不知道如何像之前使用翻译的脚本中所做的那样移动连接的对象。

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

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


TL;博士:

如何创建一个移动平台,允许顶部的物体随之移动,同时还能够独立移动并高速检测碰撞?

最佳答案

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(触发器)。当玩家位于触发区域内时,他的速度会被直接修改,并且他可以在仍然是动态刚体的情况下改变其速度。平台也对旋转和垂直轴有限制。

关于c# - Unity3D - 检测移动平台上的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64783047/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com