Advertisement
Guest User

Untitled

a guest
Oct 27th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2. #pragma implicit
  3. #pragma downcast
  4.  
  5. // Does this script currently respond to input?
  6. var canControl : boolean = true;
  7.  
  8. var useFixedUpdate : boolean = true;
  9.  
  10. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  11. // Very handy for organization!
  12.  
  13. // The current global direction we want the character to move in.
  14. @System.NonSerialized
  15. var inputMoveDirection : Vector3 = Vector3.zero;
  16.  
  17. // Is the jump button held down? We use this interface instead of checking
  18. // for the jump button directly so this script can also be used by AIs.
  19. @System.NonSerialized
  20. var inputJump : boolean = false;
  21.  
  22. class CharacterMotorMovement {
  23.     // The maximum horizontal speed when moving
  24.     var maxForwardSpeed : float = 80.0;
  25.     var maxSidewaysSpeed : float = 80.0;
  26.     var maxBackwardsSpeed : float = 80.0;
  27.    
  28.     // Curve for multiplying speed based on slope (negative = downwards)
  29.     var slopeSpeedMultiplier : AnimationCurve = AnimationCurve(Keyframe(-90, 1), Keyframe(0, 1), Keyframe(90, 0));
  30.    
  31.     // How fast does the character change speeds?  Higher is faster.
  32.     var maxGroundAcceleration : float = 30.0;
  33.     var maxAirAcceleration : float = 20.0;
  34.  
  35.     // The gravity for the character
  36.     var gravity : float = 10.0;
  37.     var maxFallSpeed : float = 20.0;
  38.    
  39.     // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  40.     // Very handy for organization!
  41.  
  42.     // The last collision flags returned from controller.Move
  43.     @System.NonSerialized
  44.     var collisionFlags : CollisionFlags;
  45.  
  46.     // We will keep track of the character's current velocity,
  47.     @System.NonSerialized
  48.     var velocity : Vector3;
  49.    
  50.     // This keeps track of our current velocity while we're not grounded
  51.     @System.NonSerialized
  52.     var frameVelocity : Vector3 = Vector3.zero;
  53.    
  54.     @System.NonSerialized
  55.     var hitPoint : Vector3 = Vector3.zero;
  56.    
  57.     @System.NonSerialized
  58.     var lastHitPoint : Vector3 = Vector3(Mathf.Infinity, 0, 0);
  59. }
  60.  
  61. var movement : CharacterMotorMovement = CharacterMotorMovement();
  62.  
  63. enum MovementTransferOnJump {
  64.     None, // The jump is not affected by velocity of floor at all.
  65.     InitTransfer, // Jump gets its initial velocity from the floor, then gradualy comes to a stop.
  66.     PermaTransfer, // Jump gets its initial velocity from the floor, and keeps that velocity until landing.
  67.     PermaLocked // Jump is relative to the movement of the last touched floor and will move together with that floor.
  68. }
  69.  
  70. // We will contain all the jumping related variables in one helper class for clarity.
  71. class CharacterMotorJumping {
  72.     // Can the character jump?
  73.     var enabled : boolean = true;
  74.  
  75.     // How high do we jump when pressing jump and letting go immediately
  76.     var baseHeight : float = 1.0;
  77.    
  78.     // We add extraHeight units (meters) on top when holding the button down longer while jumping
  79.     var extraHeight : float = 4.1;
  80.    
  81.     // How much does the character jump out perpendicular to the surface on walkable surfaces?
  82.     // 0 means a fully vertical jump and 1 means fully perpendicular.
  83.     var perpAmount : float = 0.0;
  84.    
  85.     // How much does the character jump out perpendicular to the surface on too steep surfaces?
  86.     // 0 means a fully vertical jump and 1 means fully perpendicular.
  87.     var steepPerpAmount : float = 0.5;
  88.    
  89.     // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  90.     // Very handy for organization!
  91.  
  92.     // Are we jumping? (Initiated with jump button and not grounded yet)
  93.     // To see if we are just in the air (initiated by jumping OR falling) see the grounded variable.
  94.     @System.NonSerialized
  95.     var jumping : boolean = false;
  96.    
  97.     @System.NonSerialized
  98.     var holdingJumpButton : boolean = false;
  99.  
  100.     // the time we jumped at (Used to determine for how long to apply extra jump power after jumping.)
  101.     @System.NonSerialized
  102.     var lastStartTime : float = 0.0;
  103.    
  104.     @System.NonSerialized
  105.     var lastButtonDownTime : float = -100;
  106.    
  107.     @System.NonSerialized
  108.     var jumpDir : Vector3 = Vector3.up;
  109. }
  110.  
  111. var jumping : CharacterMotorJumping = CharacterMotorJumping();
  112.  
  113. class CharacterMotorMovingPlatform {
  114.     var enabled : boolean = true;
  115.    
  116.     var movementTransfer : MovementTransferOnJump = MovementTransferOnJump.PermaTransfer;
  117.    
  118.     @System.NonSerialized
  119.     var hitPlatform : Transform;
  120.    
  121.     @System.NonSerialized
  122.     var activePlatform : Transform;
  123.    
  124.     @System.NonSerialized
  125.     var activeLocalPoint : Vector3;
  126.    
  127.     @System.NonSerialized
  128.     var activeGlobalPoint : Vector3;
  129.    
  130.     @System.NonSerialized
  131.     var activeLocalRotation : Quaternion;
  132.    
  133.     @System.NonSerialized
  134.     var activeGlobalRotation : Quaternion;
  135.    
  136.     @System.NonSerialized
  137.     var lastMatrix : Matrix4x4;
  138.    
  139.     @System.NonSerialized
  140.     var platformVelocity : Vector3;
  141.    
  142.     @System.NonSerialized
  143.     var newPlatform : boolean;
  144. }
  145.  
  146. var movingPlatform : CharacterMotorMovingPlatform = CharacterMotorMovingPlatform();
  147.  
  148. class CharacterMotorSliding {
  149.     // Does the character slide on too steep surfaces?
  150.     var enabled : boolean = true;
  151.    
  152.     // How fast does the character slide on steep surfaces?
  153.     var slidingSpeed : float = 15;
  154.    
  155.     // How much can the player control the sliding direction?
  156.     // If the value is 0.5 the player can slide sideways with half the speed of the downwards sliding speed.
  157.     var sidewaysControl : float = 1.0;
  158.    
  159.     // How much can the player influence the sliding speed?
  160.     // If the value is 0.5 the player can speed the sliding up to 150% or slow it down to 50%.
  161.     var speedControl : float = 0.4;
  162. }
  163.  
  164. var sliding : CharacterMotorSliding = CharacterMotorSliding();
  165.  
  166. @System.NonSerialized
  167. var grounded : boolean = true;
  168.  
  169. @System.NonSerialized
  170. var groundNormal : Vector3 = Vector3.zero;
  171.  
  172. private var lastGroundNormal : Vector3 = Vector3.zero;
  173.  
  174. private var tr : Transform;
  175.  
  176. private var controller : CharacterController;
  177.  
  178. function Awake () {
  179.     controller = GetComponent (CharacterController);
  180.     tr = transform;
  181. }
  182.  
  183. private function UpdateFunction () {
  184.     // We copy the actual velocity into a temporary variable that we can manipulate.
  185.     var velocity : Vector3 = movement.velocity;
  186.    
  187.     // Update velocity based on input
  188.     velocity = ApplyInputVelocityChange(velocity);
  189.    
  190.     // Apply gravity and jumping force
  191.     velocity = ApplyGravityAndJumping (velocity);
  192.    
  193.     // Moving platform support
  194.     var moveDistance : Vector3 = Vector3.zero;
  195.     if (MoveWithPlatform()) {
  196.         var newGlobalPoint : Vector3 = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint);
  197.         moveDistance = (newGlobalPoint - movingPlatform.activeGlobalPoint);
  198.         if (moveDistance != Vector3.zero)
  199.             controller.Move(moveDistance);
  200.        
  201.         // Support moving platform rotation as well:
  202.         var newGlobalRotation : Quaternion = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation;
  203.         var rotationDiff : Quaternion = newGlobalRotation * Quaternion.Inverse(movingPlatform.activeGlobalRotation);
  204.        
  205.         var yRotation = rotationDiff.eulerAngles.y;
  206.         if (yRotation != 0) {
  207.             // Prevent rotation of the local up vector
  208.             tr.Rotate(0, yRotation, 0);
  209.         }
  210.     }
  211.    
  212.     // Save lastPosition for velocity calculation.
  213.     var lastPosition : Vector3 = tr.position;
  214.    
  215.     // We always want the movement to be framerate independent.  Multiplying by Time.deltaTime does this.
  216.     var currentMovementOffset : Vector3 = velocity * Time.deltaTime;
  217.    
  218.     // Find out how much we need to push towards the ground to avoid loosing grouning
  219.     // when walking down a step or over a sharp change in slope.
  220.     var pushDownOffset : float = Mathf.Max(controller.stepOffset, Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude);
  221.     if (grounded)
  222.         currentMovementOffset -= pushDownOffset * Vector3.up;
  223.    
  224.     // Reset variables that will be set by collision function
  225.     movingPlatform.hitPlatform = null;
  226.     groundNormal = Vector3.zero;
  227.    
  228.     // Move our character!
  229.     movement.collisionFlags = controller.Move (currentMovementOffset);
  230.    
  231.     movement.lastHitPoint = movement.hitPoint;
  232.     lastGroundNormal = groundNormal;
  233.    
  234.     if (movingPlatform.enabled && movingPlatform.activePlatform != movingPlatform.hitPlatform) {
  235.         if (movingPlatform.hitPlatform != null) {
  236.             movingPlatform.activePlatform = movingPlatform.hitPlatform;
  237.             movingPlatform.lastMatrix = movingPlatform.hitPlatform.localToWorldMatrix;
  238.             movingPlatform.newPlatform = true;
  239.         }
  240.     }
  241.    
  242.     // Calculate the velocity based on the current and previous position.  
  243.     // This means our velocity will only be the amount the character actually moved as a result of collisions.
  244.     var oldHVelocity : Vector3 = new Vector3(velocity.x, 0, velocity.z);
  245.     movement.velocity = (tr.position - lastPosition) / Time.deltaTime;
  246.     var newHVelocity : Vector3 = new Vector3(movement.velocity.x, 0, movement.velocity.z);
  247.    
  248.     // The CharacterController can be moved in unwanted directions when colliding with things.
  249.     // We want to prevent this from influencing the recorded velocity.
  250.     if (oldHVelocity == Vector3.zero) {
  251.         movement.velocity = new Vector3(0, movement.velocity.y, 0);
  252.     }
  253.     else {
  254.         var projectedNewVelocity : float = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude;
  255.         movement.velocity = oldHVelocity * Mathf.Clamp01(projectedNewVelocity) + movement.velocity.y * Vector3.up;
  256.     }
  257.    
  258.     if (movement.velocity.y < velocity.y - 0.001) {
  259.         if (movement.velocity.y < 0) {
  260.             // Something is forcing the CharacterController down faster than it should.
  261.             // Ignore this
  262.             movement.velocity.y = velocity.y;
  263.         }
  264.         else {
  265.             // The upwards movement of the CharacterController has been blocked.
  266.             // This is treated like a ceiling collision - stop further jumping here.
  267.             jumping.holdingJumpButton = false;
  268.         }
  269.     }
  270.    
  271.     // We were grounded but just loosed grounding
  272.     if (grounded && !IsGroundedTest()) {
  273.         grounded = false;
  274.        
  275.         // Apply inertia from platform
  276.         if (movingPlatform.enabled &&
  277.             (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  278.             movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  279.         ) {
  280.             movement.frameVelocity = movingPlatform.platformVelocity;
  281.             movement.velocity += movingPlatform.platformVelocity;
  282.         }
  283.        
  284.         SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
  285.         // We pushed the character down to ensure it would stay on the ground if there was any.
  286.         // But there wasn't so now we cancel the downwards offset to make the fall smoother.
  287.         tr.position += pushDownOffset * Vector3.up;
  288.     }
  289.     // We were not grounded but just landed on something
  290.     else if (!grounded && IsGroundedTest()) {
  291.         grounded = true;
  292.         jumping.jumping = false;
  293.         SubtractNewPlatformVelocity();
  294.        
  295.         SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
  296.     }
  297.    
  298.     // Moving platforms support
  299.     if (MoveWithPlatform()) {
  300.         // Use the center of the lower half sphere of the capsule as reference point.
  301.         // This works best when the character is standing on moving tilting platforms.
  302.         movingPlatform.activeGlobalPoint = tr.position + Vector3.up * (controller.center.y - controller.height*0.5 + controller.radius);
  303.         movingPlatform.activeLocalPoint = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint);
  304.        
  305.         // Support moving platform rotation as well:
  306.         movingPlatform.activeGlobalRotation = tr.rotation;
  307.         movingPlatform.activeLocalRotation = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation;
  308.     }
  309. }
  310.  
  311. function FixedUpdate () {
  312.     if (movingPlatform.enabled) {
  313.         if (movingPlatform.activePlatform != null) {
  314.             if (!movingPlatform.newPlatform) {
  315.                 var lastVelocity : Vector3 = movingPlatform.platformVelocity;
  316.                
  317.                 movingPlatform.platformVelocity = (
  318.                     movingPlatform.activePlatform.localToWorldMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
  319.                     - movingPlatform.lastMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
  320.                 ) / Time.deltaTime;
  321.             }
  322.             movingPlatform.lastMatrix = movingPlatform.activePlatform.localToWorldMatrix;
  323.             movingPlatform.newPlatform = false;
  324.         }
  325.         else {
  326.             movingPlatform.platformVelocity = Vector3.zero;
  327.         }
  328.     }
  329.    
  330.     if (useFixedUpdate)
  331.         UpdateFunction();
  332. }
  333.  
  334. function Update () {
  335.     if (!useFixedUpdate)
  336.         UpdateFunction();
  337. }
  338.  
  339. private function ApplyInputVelocityChange (velocity : Vector3) {   
  340.     if (!canControl)
  341.         inputMoveDirection = Vector3.zero;
  342.    
  343.     // Find desired velocity
  344.     var desiredVelocity : Vector3;
  345.     if (grounded && TooSteep()) {
  346.         // The direction we're sliding in
  347.         desiredVelocity = Vector3(groundNormal.x, 0, groundNormal.z).normalized;
  348.         // Find the input movement direction projected onto the sliding direction
  349.         var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
  350.         // Add the sliding direction, the spped control, and the sideways control vectors
  351.         desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl;
  352.         // Multiply with the sliding speed
  353.         desiredVelocity *= sliding.slidingSpeed;
  354.     }
  355.     else
  356.         desiredVelocity = GetDesiredHorizontalVelocity();
  357.    
  358.     if (movingPlatform.enabled && movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) {
  359.         desiredVelocity += movement.frameVelocity;
  360.         desiredVelocity.y = 0;
  361.     }
  362.    
  363.     if (grounded)
  364.         desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal);
  365.     else
  366.         velocity.y = 0;
  367.    
  368.     // Enforce max velocity change
  369.     var maxVelocityChange : float = GetMaxAcceleration(grounded) * Time.deltaTime;
  370.     var velocityChangeVector : Vector3 = (desiredVelocity - velocity);
  371.     if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) {
  372.         velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange;
  373.     }
  374.     // If we're in the air and don't have control, don't apply any velocity change at all.
  375.     // If we're on the ground and don't have control we do apply it - it will correspond to friction.
  376.     if (grounded || canControl)
  377.         velocity += velocityChangeVector;
  378.    
  379.     if (grounded) {
  380.         // When going uphill, the CharacterController will automatically move up by the needed amount.
  381.         // Not moving it upwards manually prevent risk of lifting off from the ground.
  382.         // When going downhill, DO move down manually, as gravity is not enough on steep hills.
  383.         velocity.y = Mathf.Min(velocity.y, 0);
  384.     }
  385.    
  386.     return velocity;
  387. }
  388.  
  389. private function ApplyGravityAndJumping (velocity : Vector3) {
  390.    
  391.     if (!inputJump || !canControl) {
  392.         jumping.holdingJumpButton = false;
  393.         jumping.lastButtonDownTime = -100;
  394.     }
  395.    
  396.     if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
  397.         jumping.lastButtonDownTime = Time.time;
  398.    
  399.     if (grounded)
  400.         velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
  401.     else {
  402.         velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
  403.        
  404.         // When jumping up we don't apply gravity for some time when the user is holding the jump button.
  405.         // This gives more control over jump height by pressing the button longer.
  406.         if (jumping.jumping && jumping.holdingJumpButton) {
  407.             // Calculate the duration that the extra jump force should have effect.
  408.             // If we're still less than that duration after the jumping time, apply the force.
  409.             if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
  410.                 // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
  411.                 velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
  412.             }
  413.         }
  414.        
  415.         // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
  416.         velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
  417.     }
  418.        
  419.     if (grounded) {
  420.         // Jump only if the jump button was pressed down in the last 0.2 seconds.
  421.         // We use this check instead of checking if it's pressed down right now
  422.         // because players will often try to jump in the exact moment when hitting the ground after a jump
  423.         // and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
  424.         // it's confusing and it feels like the game is buggy.
  425.         if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
  426.             grounded = false;
  427.             jumping.jumping = true;
  428.             jumping.lastStartTime = Time.time;
  429.             jumping.lastButtonDownTime = -100;
  430.             jumping.holdingJumpButton = true;
  431.            
  432.             // Calculate the jumping direction
  433.             if (TooSteep())
  434.                 jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
  435.             else
  436.                 jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
  437.            
  438.             // Apply the jumping force to the velocity. Cancel any vertical velocity first.
  439.             velocity.y = 0;
  440.             velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
  441.            
  442.             // Apply inertia from platform
  443.             if (movingPlatform.enabled &&
  444.                 (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  445.                 movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  446.             ) {
  447.                 movement.frameVelocity = movingPlatform.platformVelocity;
  448.                 velocity += movingPlatform.platformVelocity;
  449.             }
  450.            
  451.             SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
  452.         }
  453.         else {
  454.             jumping.holdingJumpButton = false;
  455.         }
  456.     }
  457.    
  458.     return velocity;
  459. }
  460.  
  461. function OnControllerColliderHit (hit : ControllerColliderHit) {
  462.     if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
  463.         if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
  464.             groundNormal = hit.normal;
  465.         else
  466.             groundNormal = lastGroundNormal;
  467.        
  468.         movingPlatform.hitPlatform = hit.collider.transform;
  469.         movement.hitPoint = hit.point;
  470.         movement.frameVelocity = Vector3.zero;
  471.     }
  472. }
  473.  
  474. private function SubtractNewPlatformVelocity () {
  475.     // When landing, subtract the velocity of the new ground from the character's velocity
  476.     // since movement in ground is relative to the movement of the ground.
  477.     if (movingPlatform.enabled &&
  478.         (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  479.         movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  480.     ) {
  481.         // If we landed on a new platform, we have to wait for two FixedUpdates
  482.         // before we know the velocity of the platform under the character
  483.         if (movingPlatform.newPlatform) {
  484.             var platform : Transform = movingPlatform.activePlatform;
  485.             yield WaitForFixedUpdate();
  486.             yield WaitForFixedUpdate();
  487.             if (grounded && platform == movingPlatform.activePlatform)
  488.                 yield 1;
  489.         }
  490.         movement.velocity -= movingPlatform.platformVelocity;
  491.     }
  492. }
  493.  
  494. private function MoveWithPlatform () : boolean {
  495.     return (
  496.         movingPlatform.enabled
  497.         && (grounded || movingPlatform.movementTransfer == MovementTransferOnJump.PermaLocked)
  498.         && movingPlatform.activePlatform != null
  499.     );
  500. }
  501.  
  502. private function GetDesiredHorizontalVelocity () {
  503.     // Find desired velocity
  504.     var desiredLocalDirection : Vector3 = tr.InverseTransformDirection(inputMoveDirection);
  505.     var maxSpeed : float = MaxSpeedInDirection(desiredLocalDirection);
  506.     if (grounded) {
  507.         // Modify max speed on slopes based on slope speed multiplier curve
  508.         var movementSlopeAngle = Mathf.Asin(movement.velocity.normalized.y)  * Mathf.Rad2Deg;
  509.         maxSpeed *= movement.slopeSpeedMultiplier.Evaluate(movementSlopeAngle);
  510.     }
  511.     return tr.TransformDirection(desiredLocalDirection * maxSpeed);
  512. }
  513.  
  514. private function AdjustGroundVelocityToNormal (hVelocity : Vector3, groundNormal : Vector3) : Vector3 {
  515.     var sideways : Vector3 = Vector3.Cross(Vector3.up, hVelocity);
  516.     return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude;
  517. }
  518.  
  519. private function IsGroundedTest () {
  520.     return (groundNormal.y > 0.01);
  521. }
  522.  
  523. function GetMaxAcceleration (grounded : boolean) : float {
  524.     // Maximum acceleration on ground and in air
  525.     if (grounded)
  526.         return movement.maxGroundAcceleration;
  527.     else
  528.         return movement.maxAirAcceleration;
  529. }
  530.  
  531. function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
  532.     // From the jump height and gravity we deduce the upwards speed
  533.     // for the character to reach at the apex.
  534.     return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
  535. }
  536.  
  537. function IsJumping () {
  538.     return jumping.jumping;
  539. }
  540.  
  541. function IsSliding () {
  542.     return (grounded && sliding.enabled && TooSteep());
  543. }
  544.  
  545. function IsTouchingCeiling () {
  546.     return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0;
  547. }
  548.  
  549. function IsGrounded () {
  550.     return grounded;
  551. }
  552.  
  553. function TooSteep () {
  554.     return (groundNormal.y <= Mathf.Cos(controller.slopeLimit * Mathf.Deg2Rad));
  555. }
  556.  
  557. function GetDirection () {
  558.     return inputMoveDirection;
  559. }
  560.  
  561. function SetControllable (controllable : boolean) {
  562.     canControl = controllable;
  563. }
  564.  
  565. // Project a direction onto elliptical quater segments based on forward, sideways, and backwards speed.
  566. // The function returns the length of the resulting vector.
  567. function MaxSpeedInDirection (desiredMovementDirection : Vector3) : float {
  568.     if (desiredMovementDirection == Vector3.zero)
  569.         return 0;
  570.     else {
  571.         var zAxisEllipseMultiplier : float = (desiredMovementDirection.z > 0 ? movement.maxForwardSpeed : movement.maxBackwardsSpeed) / movement.maxSidewaysSpeed;
  572.         var temp : Vector3 = new Vector3(desiredMovementDirection.x, 0, desiredMovementDirection.z / zAxisEllipseMultiplier).normalized;
  573.         var length : float = new Vector3(temp.x, 0, temp.z * zAxisEllipseMultiplier).magnitude * movement.maxSidewaysSpeed;
  574.         return length;
  575.     }
  576. }
  577.  
  578. function SetVelocity (velocity : Vector3) {
  579.     grounded = false;
  580.     movement.velocity = velocity;
  581.     movement.frameVelocity = Vector3.zero;
  582.     SendMessage("OnExternalVelocity");
  583. }
  584.  
  585. // Require a character controller to be attached to the same game object
  586. @script RequireComponent (CharacterController)
  587. @script AddComponentMenu ("Character/Character Motor")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement