两个对象之间的夹点 [英] Grip Between Two Objects

查看:93
本文介绍了两个对象之间的夹点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在两个对象之间抓紧实际上,小方块是具有刚体的球员,大方块是帮助小方块跳到上面并继续在其他大方块上跳的对象到达目的地的多维数据集.我需要当玩家跳跃并着陆在旋转的立方体上时,因此默认情况下,它们之间应该有摩擦力,玩家应该随大立方体旋转以使其位于大立方体上.

I need grip between two objects actually small cube is a player having rigid body and big cube is an object that helps small cube to jump on it and keep jumping on other big cubes to reach to the destination. I need when the player jumps and land on rotating cube so there should friction between them by default the player should rotate with big cube cause its on the big cube.

预期结果是,具有刚体的小立方体也应与大立方体一起旋转,这是因为大立方体正在旋转并且在大立方体上:

The expected result was that the small cube having rigid body should also rotate with big cube cause big cube is rotating and is on the big cube:

推荐答案

您可以将小立方体游戏对象设置为大立方体游戏对象的子代.这应该是技巧.

You can set the small cube gameobject as child of the big cube gameobject. This should to the trick.

----在评论后进行编辑

----EDIT AFTER COMMENTS

如果您需要更改子项(因为小立方体可以移开),则需要一个脚本,该脚本可在需要时添加和删除子项.

If you need to change the child hiearchy (because the small cube can move away), then you need a script that add and remove the child when required.

=>当玩家(小方块)在大方块上时,您的子玩家会更多.

=> When player (small cube) is on the big cube you much child player to the big cube.

=>当玩家(小方块)从大方块中移开时,您的子童玩家就会移到大方块中.

=> When player (small cube) moves away of the big cube you much de-child player to the big cube.

如果您使用的是刚体,则可以使用 OnCollisionEnter OnCollisionExit .

If you're using rigidbodies you may use OnCollisionEnter and OnCollisionExit.

您可以将此单行为附加到大立方体上.

You may attach this monobehaviour to the big cube.

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

您还可以对玩家的旋转施加力,直到玩家停留在立方体上.在这种情况下,平衡旋转力非常重要(您可以在编辑器中尝试一下).

You can also apply a force to the rotation of the player until he stays on the cube. In this case it's quite important to balance the rotation force well (you can try it in the editor).

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

Unity教程

Unity教程

这篇关于两个对象之间的夹点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆