How to make capsule immerse in cube like in water?

I want to make penguin (capsule) enter the water (cube) and swim in it. In 2D.

1 Answer

1

One way of doing this would be to make the cube a trigger zone and OnTriggerStay check if the rigidbody in it it’s your penguin and if so add force to it upwards. Something like this:

function OnTriggerStay (other : Collider) {
	if (other.tag=="player")
		other.attachedRigidbody.AddForce(Vector3.up * 10);
}

Depending on your weight of your capsule you need to play with that value to make it float. But it’s simple as that.

NOTE: The example code is for 3d object, but similar can be written for 2d, just swap for the appropriate 2d functions instead.