/// <summary> /// pushes a rigidbody using the supplied 'PushForceMode' (regardless /// of the controller's own mode). if mode is 'Kinetic', the point of /// impact will automatically be the rigidbody's closest point on bounds /// to the center of the controller collider /// </summary> public void PushRigidbody(Rigidbody rigidbody, Vector3 moveDirection, PushForceMode pushForceMode) { PushRigidbody(rigidbody, moveDirection, pushForceMode, (pushForceMode == PushForceMode.Simplified ? Vector3.zero : rigidbody.ClosestPointOnBounds(Collider.bounds.center))); }
/// <summary> /// makes this controller push a rigidbody using force or fixed velocity /// depending on the supplied 'PushForceMode'. in the 'Simplified' mode /// the controller will directly set the velocity of the rigidbody. /// in the 'Kinetic' mode force will be applied to the rigidbody at point /// of contact, and will accumulate. NOTE: 'point' will only have effect /// in 'Kinetic' mode. /// </summary> public void PushRigidbody(Rigidbody rigidbody, Vector3 moveDirection, PushForceMode pushForcemode, Vector3 point) { switch (pushForcemode) { case PushForceMode.Simplified: rigidbody.velocity = (vp_3DUtility.HorizontalVector((new Vector3(moveDirection.x, 0, moveDirection.z)).normalized) * (PhysicsPushForce / rigidbody.mass)); break; case PushForceMode.Kinetic: // if collision occurs beside (neither above nor below) the player we // will only apply horizontal force. this makes pushing stuff around // much smoother and easier if (Vector3.Distance(vp_3DUtility.HorizontalVector(Transform.position), vp_3DUtility.HorizontalVector(point)) > Player.Radius.Get()) { rigidbody.AddForceAtPosition(vp_3DUtility.HorizontalVector(moveDirection) * (PhysicsPushForce * KINETIC_PUSHFORCE_MULTIPLIER), point); // DEBUG: uncomment this to visualize horizontal push RPCs as green balls //GameObject o1 = vp_3DUtility.DebugBall(); //o1.renderer.material.color = Color.green; //o1.transform.position = point; } else { // if collision occured above or below the player we will apply force // along the unmodified collision vector. this makes for more realistic // physics when walking on top of stuff or bumping your head into it rigidbody.AddForceAtPosition(moveDirection * (PhysicsPushForce * KINETIC_PUSHFORCE_MULTIPLIER), point); // DEBUG: uncomment this to visualize vertical push RPCs as red balls //GameObject o2 = vp_3DUtility.DebugBall(); //o2.transform.position = point; } break; } }