----评论后编辑
如果需要更改子层次结构(因为小立方体可以移动),则需要编写脚本在需要时添加和删除子对象。
=> 当玩家(小立方体)在大立方体上时,您需要将玩家作为子对象添加到大立方体中。
=> 当玩家(小立方体)移开大立方体时,您需要将玩家从大立方体中删除。
如果您正在使用刚体,则可以使用 OnCollisionEnter 和 OnCollisionExit。
您可以将此monobehaviour附加到大立方体上。
public class BigCubeScript : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
//check if the colliding object is player (here I'm using a tag, but you may check it as you prefer)
if (other.gameObject.tag == "Player")
//change the parent of the player, setting it as this cube
other.transform.SetParent(this.transform);
}
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "Player")
//remove the player from the cube
other.transform.SetParent(null);
}
}
public class BigCubeScript : MonoBehaviour
{
//you may change this to add or remove the force
Vector3 _rotationForce = new Vector3(0, 5, 0);
private void OnCollisionStay(Collision other)
{
var rigidbody = other.gameObject.GetComponent<Rigidbody>();
Quaternion deltaRotation = Quaternion.Euler(_rotationForce * Time.deltaTime);
rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
}
}