topical media & game development

talk show tell print

lib-unity-tutorial-platform-start-Assets-Scripts-Player-ThirdPersonPushBodies.js / js



  var pushPower = 0.5;
  var pushLayers : LayerMask = -1;
  private var controller : ThirdPersonController;
  
  @script RequireComponent(ThirdPersonController)
  
  function Start ()
  {
          controller = GetComponent (ThirdPersonController);
  }
  
  function OnControllerColliderHit (hit : ControllerColliderHit)
  {
          var body : Rigidbody = hit.collider.attachedRigidbody;
          // no rigidbody
          if (body == null || body.isKinematic)
                  return;
          // Ignore pushing those rigidbodies
          var bodyLayerMask = 1 << body.gameObject.layer;
          if ((bodyLayerMask & pushLayers.value) == 0)
                  return;
                  
          // We dont want to push objects below us
          if (hit.moveDirection.y < -0.3) 
                  return;
          
          // Calculate push direction from move direction, we only push objects to the sides
          // never up and down
          var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
          
          // push with move speed but never more than walkspeed
          body.velocity = pushDir * pushPower * Mathf.Min(controller.GetSpeed(), controller.walkSpeed);
  }


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.