Pushing/pulling a 3D object using physics
I'm making a 3D puzzle platformer game. I'm using physics to control my character . I want my character to be able to push and pull blocks, but only on one axis. I also want another character to stand on the block to move him to a high ledge, but he just floats in the air because the block isn't kinematic.
What should I do?
I tried using the below code, but still no luck.
public float pushPower = 2.0f;
void OnCollisionEnter(Collision hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.transform.position.y < -0.3f)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
Vector3 pushDir = new Vector3(hit.transform.position.x, 0, hit.transform.position.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * pushPower;
}
Comment