Advertisement
Guest User

NPCMotor.js

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