3
votes

I need grip between two objectsenter image description here 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:

my scene

2

2 Answers

2
votes

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

enter image description here

----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.

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

Further info in OnCollisioEnter and OnCollisionExit in this Unity Tutorial

Further info on the Tags in this Unity Tutorial

1
votes

You could try to constrain position and rotation on Rigidbody of the small cube. After that you could call constrains.none so that you could allow jump again and do that every time the small cube collides with the big one. Hope it helps :)