Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Y need help ! is eazy

Discussion in 'Scripting' started by exgengames, Dec 6, 2012.

  1. exgengames

    exgengames

    Joined:
    Aug 10, 2012
    Posts:
    31
    I need help to add the variable fall damage Enhanced fpswalker style

    eazy is used please and photon multiplayer game


    Here is http://wiki.unity3d.com/index.php?title=FPSWalkerEnhanced


    Is .js
    Code (csharp):
    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. //Check when run, walk or when can run or not
    11. @HideInInspector
    12. public var Running : boolean;
    13. @HideInInspector
    14. public var Walking : boolean;
    15. @HideInInspector
    16. public var canRun : boolean;
    17.  
    18. //Ladder variables
    19. private var mainCamera : GameObject = null;
    20. @HideInInspector
    21. public var onLadder = false;
    22. private var ladderHopSpeed = 6.0;
    23.  
    24. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
    25. // Very handy for organization!
    26.  
    27. // The current global direction we want the character to move in.
    28. @System.NonSerialized
    29. var inputMoveDirection : Vector3 = Vector3.zero;
    30.  
    31. // Is the jump button held down? We use this interface instead of checking
    32. // for the jump button directly so this script can also be used by AIs.
    33. @System.NonSerialized
    34. var inputJump : boolean = false;
    35.  
    36. @HideInInspector
    37. var inputRun : boolean = false;
    38.  
    39. @HideInInspector
    40. var inputCrouch : boolean = false;
    41.  
    42. @HideInInspector
    43. var inputProne : boolean = false;
    44.  
    45. class  FPScontrollerMovement {
    46.     // The maximum horizontal speed when moving
    47.     @HideInInspector
    48.     var maxForwardSpeed : float = 10.0;
    49.     @HideInInspector
    50.     var maxSidewaysSpeed : float = 10.0;
    51.     @HideInInspector
    52.     var maxBackwardsSpeed : float = 10.0;
    53.  
    54.     //Run and walk variables
    55.     var WalkSpeed : float = 6.0;
    56.     var RunSpeed : float = 9.0;
    57.     //Crouch
    58.     var canCrouch : boolean = true;
    59.     var CrouchSpeed : float = 3.0;
    60.     var crouchHeight : float = 1.5;
    61.     var crouchSmooth : float = 8;
    62.     //prone
    63.     var canProne : boolean = true;
    64.     var ProneSpeed : float = 1.5;
    65.     var proneHeight : float = 0.7;
    66.  
    67.     // Curve for multiplying speed based on slope (negative = downwards)
    68.     var slopeSpeedMultiplier : AnimationCurve = AnimationCurve(Keyframe(-90, 1), Keyframe(0, 1), Keyframe(90, 0));
    69.    
    70.     // How fast does the character change speeds?  Higher is faster.
    71.     var maxGroundAcceleration : float = 30.0;
    72.     var maxAirAcceleration : float = 20.0;
    73.  
    74.     // The gravity for the character
    75.     var gravity : float = 10.0;
    76.     var maxFallSpeed : float = 20.0;
    77.    
    78.     @HideInInspector
    79.     var enableGravity : boolean = true;
    80.    
    81.     // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
    82.     // Very handy for organization!
    83.  
    84.     // The last collision flags returned from controller.Move
    85.     @System.NonSerialized
    86.     var collisionFlags : CollisionFlags;
    87.  
    88.     // We will keep track of the character's current velocity,
    89.     @System.NonSerialized
    90.     var velocity : Vector3;
    91.    
    92.     // This keeps track of our current velocity while we're not grounded
    93.     @System.NonSerialized
    94.     var frameVelocity : Vector3 = Vector3.zero;
    95.    
    96.     @System.NonSerialized
    97.     var hitPoint : Vector3 = Vector3.zero;
    98.    
    99.     @System.NonSerialized
    100.     var lastHitPoint : Vector3 = Vector3(Mathf.Infinity, 0, 0);
    101. }
    102.  
    103. var movement : FPScontrollerMovement = FPScontrollerMovement();
    104.  
    105. //Crouching vars
    106. @HideInInspector
    107. public var crouch : boolean;
    108. private var standartHeight : float;
    109. private var lookObj : GameObject;
    110. private var centerY : float;
    111. private var canStand : boolean;
    112. private var canStandCrouch : boolean = true;
    113.  
    114. //Prone vars
    115. @HideInInspector
    116. public var prone : boolean;
    117.  
    118. enum FPSMovementTransferOnJump {
    119.     None, // The jump is not affected by velocity of floor at all.
    120.     InitTransfer, // Jump gets its initial velocity from the floor, then gradualy comes to a stop.
    121.     PermaTransfer, // Jump gets its initial velocity from the floor, and keeps that velocity until landing.
    122.     PermaLocked // Jump is relative to the movement of the last touched floor and will move together with that floor.
    123. }
    124.  
    125. // We will contain all the jumping related variables in one helper class for clarity.
    126. class FPScontrollerJumping {
    127.     // Can the character jump?
    128.     var enabled : boolean = true;
    129.  
    130.     // How high do we jump when pressing jump and letting go immediately
    131.     var baseHeight : float = 1.0;
    132.    
    133.     // We add extraHeight units (meters) on top when holding the button down longer while jumping
    134.     var extraHeight : float = 4.1;
    135.    
    136.     // How much does the character jump out perpendicular to the surface on walkable surfaces?
    137.     // 0 means a fully vertical jump and 1 means fully perpendicular.
    138.     var perpAmount : float = 0.0;
    139.    
    140.     // How much does the character jump out perpendicular to the surface on too steep surfaces?
    141.     // 0 means a fully vertical jump and 1 means fully perpendicular.
    142.     var steepPerpAmount : float = 0.5;
    143.    
    144.     // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
    145.     // Very handy for organization!
    146.  
    147.     // Are we jumping? (Initiated with jump button and not grounded yet)
    148.     // To see if we are just in the air (initiated by jumping OR falling) see the grounded variable.
    149.     @System.NonSerialized
    150.     var jumping : boolean = false;
    151.    
    152.     @System.NonSerialized
    153.     var holdingJumpButton : boolean = false;
    154.  
    155.     // the time we jumped at (Used to determine for how long to apply extra jump power after jumping.)
    156.     @System.NonSerialized
    157.     var lastStartTime : float = 0.0;
    158.    
    159.     @System.NonSerialized
    160.     var lastButtonDownTime : float = -100;
    161.    
    162.     @System.NonSerialized
    163.     var jumpDir : Vector3 = Vector3.up;
    164. }
    165.  
    166. var jumping : FPScontrollerJumping = FPScontrollerJumping();
    167.  
    168. class FPScontrollerMovingPlatform {
    169.     var enabled : boolean = true;
    170.    
    171.     var movementTransfer : FPSMovementTransferOnJump = FPSMovementTransferOnJump.PermaTransfer;
    172.    
    173.     @System.NonSerialized
    174.     var hitPlatform : Transform;
    175.    
    176.     @System.NonSerialized
    177.     var activePlatform : Transform;
    178.    
    179.     @System.NonSerialized
    180.     var activeLocalPoint : Vector3;
    181.    
    182.     @System.NonSerialized
    183.     var activeGlobalPoint : Vector3;
    184.    
    185.     @System.NonSerialized
    186.     var activeLocalRotation : Quaternion;
    187.    
    188.     @System.NonSerialized
    189.     var activeGlobalRotation : Quaternion;
    190.    
    191.     @System.NonSerialized
    192.     var lastMatrix : Matrix4x4;
    193.    
    194.     @System.NonSerialized
    195.     var platformVelocity : Vector3;
    196.    
    197.     @System.NonSerialized
    198.     var newPlatform : boolean;
    199. }
    200.  
    201. var movingPlatform : FPScontrollerMovingPlatform = FPScontrollerMovingPlatform();
    202.  
    203. class FPScontrollerSliding {
    204.     // Does the character slide on too steep surfaces?
    205.     var enabled : boolean = true;
    206.    
    207.     // How fast does the character slide on steep surfaces?
    208.     var slidingSpeed : float = 15;
    209.    
    210.     // How much can the player control the sliding direction?
    211.     // If the value is 0.5 the player can slide sideways with half the speed of the downwards sliding speed.
    212.     var sidewaysControl : float = 1.0;
    213.    
    214.     // How much can the player influence the sliding speed?
    215.     // If the value is 0.5 the player can speed the sliding up to 150% or slow it down to 50%.
    216.     var speedControl : float = 0.4;
    217. }
    218.  
    219. var sliding : FPScontrollerSliding = FPScontrollerSliding();
    220.  
    221. class FPScontrollerPushing {   
    222.     //Push RigidBodies
    223.     var canPush : boolean = true;
    224.     var pushPower = 2.0;
    225. }
    226. var pushing : FPScontrollerPushing;
    227.  
    228. @System.NonSerialized
    229. var grounded : boolean = true;
    230.  
    231. @System.NonSerialized
    232. var groundNormal : Vector3 = Vector3.zero;
    233.  
    234. private var lastGroundNormal : Vector3 = Vector3.zero;
    235.  
    236. private var tr : Transform;
    237.  
    238. private var controller : CharacterController;
    239.  
    240. function Awake () {
    241.     controller = GetComponent (CharacterController);
    242.     standartHeight = controller.height;
    243.     lookObj = GameObject.FindWithTag("LookObject");
    244.     centerY = controller.center.y;
    245.     tr = transform;
    246.     mainCamera = GameObject.FindWithTag("MainCamera");
    247.     canRun = true;
    248.     canStand = true;
    249.     setupBools();
    250. }
    251.  
    252. function setupBools(){
    253.     if(!movement.canProne){
    254.         movement.canProne = true;
    255.         yield WaitForSeconds(0.2);
    256.         movement.canProne = false;
    257.     }
    258. }
    259.  
    260. private function UpdateFunction () {
    261.     // We copy the actual velocity into a temporary variable that we can manipulate.
    262.     var velocity : Vector3 = movement.velocity;
    263.    
    264.     // Update velocity based on input
    265.     velocity = ApplyInputVelocityChange(velocity);
    266.    
    267.     // Apply gravity and jumping force
    268.     if(movement.enableGravity){
    269.         if((prone || crouch)  inputJump)
    270.             return;
    271.         velocity = ApplyGravityAndJumping (velocity);
    272.     }
    273.     // Moving platform support
    274.     var moveDistance : Vector3 = Vector3.zero;
    275.     if (MoveWithPlatform()) {
    276.         var newGlobalPoint : Vector3 = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint);
    277.         moveDistance = (newGlobalPoint - movingPlatform.activeGlobalPoint);
    278.         if (moveDistance != Vector3.zero)
    279.             controller.Move(moveDistance);
    280.        
    281.         // Support moving platform rotation as well:
    282.         var newGlobalRotation : Quaternion = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation;
    283.         var rotationDiff : Quaternion = newGlobalRotation * Quaternion.Inverse(movingPlatform.activeGlobalRotation);
    284.        
    285.         var yRotation = rotationDiff.eulerAngles.y;
    286.         if (yRotation != 0) {
    287.             // Prevent rotation of the local up vector
    288.             tr.Rotate(0, yRotation, 0);
    289.         }
    290.     }
    291.    
    292.     // Save lastPosition for velocity calculation.
    293.     var lastPosition : Vector3 = tr.position;
    294.    
    295.     // We always want the movement to be framerate independent.  Multiplying by Time.deltaTime does this.
    296.     var currentMovementOffset : Vector3 = velocity * Time.deltaTime;
    297.    
    298.     // Find out how much we need to push towards the ground to avoid loosing grouning
    299.     // when walking down a step or over a sharp change in slope.
    300.     var pushDownOffset : float = Mathf.Max(controller.stepOffset, Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude);
    301.     if (grounded)
    302.         currentMovementOffset -= pushDownOffset * Vector3.up;
    303.    
    304.     // Reset variables that will be set by collision function
    305.     movingPlatform.hitPlatform = null;
    306.     groundNormal = Vector3.zero;
    307.    
    308.     // Move our character!
    309.     movement.collisionFlags = controller.Move (currentMovementOffset);
    310.    
    311.     movement.lastHitPoint = movement.hitPoint;
    312.     lastGroundNormal = groundNormal;
    313.    
    314.     if (movingPlatform.enabled  movingPlatform.activePlatform != movingPlatform.hitPlatform) {
    315.         if (movingPlatform.hitPlatform != null) {
    316.             movingPlatform.activePlatform = movingPlatform.hitPlatform;
    317.             movingPlatform.lastMatrix = movingPlatform.hitPlatform.localToWorldMatrix;
    318.             movingPlatform.newPlatform = true;
    319.         }
    320.     }
    321.    
    322.     // Calculate the velocity based on the current and previous position.  
    323.     // This means our velocity will only be the amount the character actually moved as a result of collisions.
    324.     var oldHVelocity : Vector3 = new Vector3(velocity.x, 0, velocity.z);
    325.     movement.velocity = (tr.position - lastPosition) / Time.deltaTime;
    326.     var newHVelocity : Vector3 = new Vector3(movement.velocity.x, 0, movement.velocity.z);
    327.    
    328.     // The CharacterController can be moved in unwanted directions when colliding with things.
    329.     // We want to prevent this from influencing the recorded velocity.
    330.     if (oldHVelocity == Vector3.zero) {
    331.         movement.velocity = new Vector3(0, movement.velocity.y, 0);
    332.     }
    333.     else {
    334.         var projectedNewVelocity : float = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude;
    335.         movement.velocity = oldHVelocity * Mathf.Clamp01(projectedNewVelocity) + movement.velocity.y * Vector3.up;
    336.     }
    337.    
    338.     if (movement.velocity.y < velocity.y - 0.001) {
    339.         if (movement.velocity.y < 0) {
    340.             // Something is forcing the CharacterController down faster than it should.
    341.             // Ignore this
    342.             movement.velocity.y = velocity.y;
    343.         }
    344.         else {
    345.             // The upwards movement of the CharacterController has been blocked.
    346.             // This is treated like a ceiling collision - stop further jumping here.
    347.             jumping.holdingJumpButton = false;
    348.         }
    349.     }
    350.    
    351.     // We were grounded but just loosed grounding
    352.     if (grounded  !IsGroundedTest()) {
    353.         grounded = false;
    354.        
    355.         // Apply inertia from platform
    356.         if (movingPlatform.enabled
    357.             (movingPlatform.movementTransfer == FPSMovementTransferOnJump.InitTransfer ||
    358.             movingPlatform.movementTransfer == FPSMovementTransferOnJump.PermaTransfer)
    359.         ) {
    360.             movement.frameVelocity = movingPlatform.platformVelocity;
    361.             movement.velocity += movingPlatform.platformVelocity;
    362.         }
    363.        
    364.         SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
    365.         // We pushed the character down to ensure it would stay on the ground if there was any.
    366.         // But there wasn't so now we cancel the downwards offset to make the fall smoother.
    367.         tr.position += pushDownOffset * Vector3.up;
    368.     }
    369.     // We were not grounded but just landed on something
    370.     else if (!grounded  IsGroundedTest()) {
    371.         grounded = true;
    372.         jumping.jumping = false;
    373.         SubtractNewPlatformVelocity();
    374.        
    375.         SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
    376.     }
    377.    
    378.     // Moving platforms support
    379.     if (MoveWithPlatform()) {
    380.         // Use the center of the lower half sphere of the capsule as reference point.
    381.         // This works best when the character is standing on moving tilting platforms.
    382.         movingPlatform.activeGlobalPoint = tr.position + Vector3.up * (controller.center.y - controller.height*0.5 + controller.radius);
    383.         movingPlatform.activeLocalPoint = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint);
    384.        
    385.         // Support moving platform rotation as well:
    386.         movingPlatform.activeGlobalRotation = tr.rotation;
    387.         movingPlatform.activeLocalRotation = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation;
    388.     }
    389. }
    390.  
    391. function FixedUpdate () {
    392.     if (movingPlatform.enabled) {
    393.         if (movingPlatform.activePlatform != null) {
    394.             if (!movingPlatform.newPlatform) {
    395.                 var lastVelocity : Vector3 = movingPlatform.platformVelocity;
    396.                
    397.                 movingPlatform.platformVelocity = (
    398.                     movingPlatform.activePlatform.localToWorldMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
    399.                     - movingPlatform.lastMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
    400.                 ) / Time.deltaTime;
    401.             }
    402.             movingPlatform.lastMatrix = movingPlatform.activePlatform.localToWorldMatrix;
    403.             movingPlatform.newPlatform = false;
    404.         }
    405.         else {
    406.             movingPlatform.platformVelocity = Vector3.zero;
    407.         }
    408.     }
    409.    
    410.     if (useFixedUpdate){
    411.         UpdateFunction();
    412.     }
    413. }
    414.  
    415. function Update () {
    416.     if (!useFixedUpdate){
    417.         UpdateFunction();
    418.     }
    419.    
    420.     //Run input
    421.     if(Input.GetAxis("Vertical") > 0.1  inputRun  canRun  !onLadder  Walking){
    422.         if(canStand  canStandCrouch){
    423.             OnRunning();
    424.         }
    425.     }else{
    426.         OffRunning();
    427.     }  
    428.        
    429.     //Check when walk or not
    430.     if ((movement.velocity.x || movement.velocity.z) > 0.01 || (movement.velocity.x || movement.velocity.z) < -0.01) {
    431.         Walking = true;
    432.     }else{
    433.         Walking = false;
    434.     }
    435.    
    436.     if(!canControl)
    437.         return;
    438.    
    439.     if(movement.canCrouch){
    440.         if(!onLadder){ 
    441.             Crouch();
    442.         }
    443.     }
    444.    
    445.     if(movement.canProne){
    446.         if(!onLadder){ 
    447.             Prone();
    448.         }
    449.     }
    450.    
    451.     if(onLadder){
    452.         grounded = false;
    453.         crouch = false;
    454.         prone = false;
    455.     }
    456.    
    457.     if(!crouch  !prone  controller.height < standartHeight-0.01){
    458.         controller.height = Mathf.Lerp(controller.height, standartHeight, Time.deltaTime/movement.crouchSmooth);
    459.         controller.center.y = Mathf.Lerp(controller.center.y, centerY, Time.deltaTime/movement.crouchSmooth);
    460.         lookObj.transform.localPosition.y = Mathf.Lerp(lookObj.transform.localPosition.y, standartHeight, Time.deltaTime/movement.crouchSmooth);
    461.     }
    462. }
    463.  
    464. function Prone(){
    465.     var up = transform.TransformDirection(Vector3.up);
    466.     var hit : RaycastHit;    
    467.     var charCtrl : CharacterController = GetComponent(CharacterController);
    468.     var p1 : Vector3 = transform.position;
    469.     //Input control
    470.     if(inputProne  !Running  !onLadder  (canStand || crouch)){
    471.         crouch = false;
    472.         prone = !prone;
    473.         if(!prone){
    474.             crouch = true; 
    475.         }else{
    476.             canStandCrouch = true;
    477.         }
    478.         if(canStandCrouch){
    479.             crouch = false;
    480.         }
    481.     }
    482.    
    483.     if(inputJump  prone){
    484.         if(canStand){
    485.             prone = false;
    486.             crouch = true;
    487.             if(canStandCrouch){
    488.                 crouch = false;
    489.             }else{
    490.                 crouch = true;
    491.             }
    492.         }
    493.     }
    494.    
    495.     if(prone || Running){
    496.         if (!Physics.SphereCast (p1, charCtrl.radius, transform.up, hit, movement.crouchHeight*0.9)) {
    497.             if(Running  prone){
    498.                 prone = false;
    499.                 if(!prone){
    500.                     crouch = true; 
    501.                 }
    502.                 if(canStandCrouch){
    503.                     crouch = false;
    504.                 }
    505.             }
    506.             if(prone){
    507.                 canStand = true;
    508.             }
    509.         }else{
    510.             if(prone){
    511.                 canStand = false;
    512.             }
    513.            //print("We hit something");
    514.         }
    515.     }
    516.    
    517.     if(prone  !crouch){
    518.         if(controller.height > movement.proneHeight+0.01){
    519.             controller.height = Mathf.Lerp(controller.height, movement.proneHeight, Time.deltaTime/movement.crouchSmooth);
    520.             controller.center.y = Mathf.Lerp(controller.center.y, movement.proneHeight/2, Time.deltaTime/movement.crouchSmooth);
    521.             lookObj.transform.localPosition.y = Mathf.Lerp(lookObj.transform.localPosition.y, movement.proneHeight, Time.deltaTime/movement.crouchSmooth);
    522.             movement.maxForwardSpeed = movement.ProneSpeed;
    523.             movement.maxSidewaysSpeed = movement.ProneSpeed;
    524.             movement.maxBackwardsSpeed = movement.ProneSpeed;
    525.         }
    526.     }
    527. }
    528.  
    529. function Crouch(){
    530.     var up = transform.TransformDirection(Vector3.up);
    531.     var hit : RaycastHit;    
    532.     var charCtrl : CharacterController = GetComponent(CharacterController);
    533.     var p1 : Vector3 = transform.position;
    534.     //Input control
    535.     if(inputCrouch  ! Running  !onLadder  canStand){
    536.         prone = false;
    537.         crouch = !crouch;
    538.     }
    539.  
    540.     //if(crouch){
    541.          if (!Physics.SphereCast (p1, charCtrl.radius, transform.up, hit, standartHeight)) {
    542.             if(inputJump  crouch){
    543.                 crouch = false;
    544.             }
    545.             if(Running  crouch){
    546.                 crouch = false;
    547.             }
    548.             if(crouch){
    549.                 canStand = true;
    550.             }
    551.             canStandCrouch = true;
    552.         }else{
    553.             if(crouch){
    554.                 canStand = false;
    555.             }
    556.             canStandCrouch = false;
    557.             //print("We hit something");
    558.         }
    559.     //}
    560.    
    561.     if(crouch  !prone){
    562.         if(controller.height < movement.crouchHeight+0.01  controller.height > movement.crouchHeight-0.01)
    563.             return;
    564.         controller.height = Mathf.Lerp(controller.height, movement.crouchHeight, Time.deltaTime/movement.crouchSmooth);
    565.         controller.center.y = Mathf.Lerp(controller.center.y, movement.crouchHeight/2, Time.deltaTime/movement.crouchSmooth);
    566.         lookObj.transform.localPosition.y = Mathf.Lerp(lookObj.transform.localPosition.y, movement.crouchHeight, Time.deltaTime/movement.crouchSmooth);
    567.         movement.maxForwardSpeed = movement.CrouchSpeed;
    568.         movement.maxSidewaysSpeed = movement.CrouchSpeed;
    569.         movement.maxBackwardsSpeed = movement.CrouchSpeed;
    570.     }
    571. }
    572.  
    573. function OnRunning (){
    574.     Running = true;
    575.     movement.maxForwardSpeed = movement.RunSpeed;
    576.     movement.maxSidewaysSpeed = movement.RunSpeed;
    577.     //Make bigger extra height when player run to increase jump distance
    578.     jumping.extraHeight = jumping.baseHeight + 0.15;
    579. }
    580.  
    581. function OffRunning (){
    582.     Running = false;
    583.     if(crouch || prone)
    584.         return;
    585.     movement.maxForwardSpeed = movement.WalkSpeed;
    586.     movement.maxSidewaysSpeed = movement.WalkSpeed;
    587.     movement.maxBackwardsSpeed = movement.WalkSpeed/2;
    588.     //Change extraheight value to default when player walk
    589.     jumping.extraHeight = jumping.baseHeight;
    590. }
    591.  
    592. //Ladder functions
    593. function OnLadder () {
    594.     onLadder = true;
    595.     //moveDirection = Vector3.zero;
    596.     inputMoveDirection = Vector3.zero;
    597.     //Disable gravity when climb on ladder
    598.     movement.enableGravity = false;
    599. }
    600.  
    601. function OffLadder (ladderMovement) {
    602.     onLadder = false;
    603.     // perform off-ladder hop
    604.     var hop : Vector3 = mainCamera.transform.forward;
    605.     hop = transform.TransformDirection(hop);
    606.     //Enable gravity when climb off ladder
    607.     movement.enableGravity = true;
    608. }
    609.  
    610. private function ApplyInputVelocityChange (velocity : Vector3) {   
    611.     if (!canControl)
    612.         inputMoveDirection = Vector3.zero;
    613.    
    614.     // Find desired velocity
    615.     var desiredVelocity : Vector3;
    616.     if (grounded  TooSteep()) {
    617.         // The direction we're sliding in
    618.         desiredVelocity = Vector3(groundNormal.x, 0, groundNormal.z).normalized;
    619.         // Find the input movement direction projected onto the sliding direction
    620.         var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
    621.         // Add the sliding direction, the spped control, and the sideways control vectors
    622.         desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl;
    623.         // Multiply with the sliding speed
    624.         desiredVelocity *= sliding.slidingSpeed;
    625.     }
    626.     else
    627.         desiredVelocity = GetDesiredHorizontalVelocity();
    628.    
    629.     if (movingPlatform.enabled  movingPlatform.movementTransfer == FPSMovementTransferOnJump.PermaTransfer) {
    630.         desiredVelocity += movement.frameVelocity;
    631.         desiredVelocity.y = 0;
    632.     }
    633.    
    634.     if (grounded)
    635.         desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal);
    636.     else
    637.         velocity.y = 0;
    638.    
    639.     // Enforce max velocity change
    640.     var maxVelocityChange : float = GetMaxAcceleration(grounded) * Time.deltaTime;
    641.     var velocityChangeVector : Vector3 = (desiredVelocity - velocity);
    642.     if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) {
    643.         velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange;
    644.     }
    645.     // If we're in the air and don't have control, don't apply any velocity change at all.
    646.     // If we're on the ground and don't have control we do apply it - it will correspond to friction.
    647.     if (grounded || canControl)
    648.         velocity += velocityChangeVector;
    649.    
    650.     if (grounded) {
    651.         // When going uphill, the CharacterController will automatically move up by the needed amount.
    652.         // Not moving it upwards manually prevent risk of lifting off from the ground.
    653.         // When going downhill, DO move down manually, as gravity is not enough on steep hills.
    654.         velocity.y = Mathf.Min(velocity.y, 0);
    655.     }
    656.    
    657.     return velocity;
    658. }
    659.  
    660. private function ApplyGravityAndJumping (velocity : Vector3) {
    661.    
    662.     if (!inputJump || !canControl) {
    663.         jumping.holdingJumpButton = false;
    664.         jumping.lastButtonDownTime = -100;
    665.     }
    666.    
    667.     if (inputJump  jumping.lastButtonDownTime < 0  canControl)
    668.         jumping.lastButtonDownTime = Time.time;
    669.    
    670.     if (grounded)
    671.         velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
    672.     else {
    673.         velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
    674.        
    675.         // When jumping up we don't apply gravity for some time when the user is holding the jump button.
    676.         // This gives more control over jump height by pressing the button longer.
    677.         if (jumping.jumping  jumping.holdingJumpButton) {
    678.             // Calculate the duration that the extra jump force should have effect.
    679.             // If we're still less than that duration after the jumping time, apply the force.
    680.             if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
    681.                 // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
    682.                 velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
    683.             }
    684.         }
    685.        
    686.         // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
    687.         velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
    688.     }
    689.        
    690.     if (grounded) {
    691.         // Jump only if the jump button was pressed down in the last 0.2 seconds.
    692.         // We use this check instead of checking if it's pressed down right now
    693.         // because players will often try to jump in the exact moment when hitting the ground after a jump
    694.         // and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
    695.         // it's confusing and it feels like the game is buggy.
    696.         if (jumping.enabled  canControl  (Time.time - jumping.lastButtonDownTime < 0.2)) {
    697.             grounded = false;
    698.             jumping.jumping = true;
    699.             jumping.lastStartTime = Time.time;
    700.             jumping.lastButtonDownTime = -100;
    701.             jumping.holdingJumpButton = true;
    702.            
    703.             // Calculate the jumping direction
    704.             if (TooSteep())
    705.                 jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
    706.             else
    707.                 jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
    708.            
    709.             // Apply the jumping force to the velocity. Cancel any vertical velocity first.
    710.             velocity.y = 0;
    711.             velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
    712.            
    713.             // Apply inertia from platform
    714.             if (movingPlatform.enabled
    715.                 (movingPlatform.movementTransfer == FPSMovementTransferOnJump.InitTransfer ||
    716.                 movingPlatform.movementTransfer == FPSMovementTransferOnJump.PermaTransfer)
    717.             ) {
    718.                 movement.frameVelocity = movingPlatform.platformVelocity;
    719.                 velocity += movingPlatform.platformVelocity;
    720.             }
    721.            
    722.             SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
    723.         }
    724.         else {
    725.             jumping.holdingJumpButton = false;
    726.         }
    727.     }
    728.    
    729.     return velocity;
    730. }
    731.  
    732. function OnControllerColliderHit (hit : ControllerColliderHit) {
    733.     if (hit.normal.y > 0  hit.normal.y > groundNormal.y  hit.moveDirection.y < 0) {
    734.         if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
    735.             groundNormal = hit.normal;
    736.         else
    737.             groundNormal = lastGroundNormal;
    738.        
    739.         movingPlatform.hitPlatform = hit.collider.transform;
    740.         movement.hitPoint = hit.point;
    741.         movement.frameVelocity = Vector3.zero;
    742.     }
    743.    
    744.     //Did we push something?
    745.     if(pushing.canPush){
    746.         var body : Rigidbody = hit.collider.attachedRigidbody;
    747.         // no rigidbody
    748.         if (body == null || body.isKinematic)
    749.             return;
    750.  
    751.         // We dont want to push objects below us
    752.         if (hit.moveDirection.y < -0.3)
    753.             return;
    754.  
    755.         // Calculate push direction from move direction,
    756.         // we only push objects to the sides never up and down
    757.         var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
    758.    
    759.         // If you know how fast your character is trying to move,
    760.         // then you can also multiply the push velocity by that.
    761.        
    762.         // Apply the push
    763.         body.velocity = pushDir * pushing.pushPower;
    764.     }
    765. }
    766.  
    767. private function SubtractNewPlatformVelocity () {
    768.     // When landing, subtract the velocity of the new ground from the character's velocity
    769.     // since movement in ground is relative to the movement of the ground.
    770.     if (movingPlatform.enabled
    771.         (movingPlatform.movementTransfer == FPSMovementTransferOnJump.InitTransfer ||
    772.         movingPlatform.movementTransfer == FPSMovementTransferOnJump.PermaTransfer)
    773.     ) {
    774.         // If we landed on a new platform, we have to wait for two FixedUpdates
    775.         // before we know the velocity of the platform under the character
    776.         if (movingPlatform.newPlatform) {
    777.             var platform : Transform = movingPlatform.activePlatform;
    778.             yield WaitForFixedUpdate();
    779.             yield WaitForFixedUpdate();
    780.             if (grounded  platform == movingPlatform.activePlatform)
    781.                 yield 1;
    782.         }
    783.         movement.velocity -= movingPlatform.platformVelocity;
    784.     }
    785. }
    786.  
    787. private function MoveWithPlatform () : boolean {
    788.     return (
    789.         movingPlatform.enabled
    790.          (grounded || movingPlatform.movementTransfer == FPSMovementTransferOnJump.PermaLocked)
    791.          movingPlatform.activePlatform != null
    792.     );
    793. }
    794.  
    795. private function GetDesiredHorizontalVelocity () {
    796.     // Find desired velocity
    797.     var desiredLocalDirection : Vector3 = tr.InverseTransformDirection(inputMoveDirection);
    798.     var maxSpeed : float = MaxSpeedInDirection(desiredLocalDirection);
    799.     if (grounded) {
    800.         // Modify max speed on slopes based on slope speed multiplier curve
    801.         var movementSlopeAngle = Mathf.Asin(movement.velocity.normalized.y)  * Mathf.Rad2Deg;
    802.         maxSpeed *= movement.slopeSpeedMultiplier.Evaluate(movementSlopeAngle);
    803.     }
    804.     return tr.TransformDirection(desiredLocalDirection * maxSpeed);
    805. }
    806.  
    807. private function AdjustGroundVelocityToNormal (hVelocity : Vector3, groundNormal : Vector3) : Vector3 {
    808.     var sideways : Vector3 = Vector3.Cross(Vector3.up, hVelocity);
    809.     return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude;
    810. }
    811.  
    812. private function IsGroundedTest () {
    813.     return (groundNormal.y > 0.01);
    814. }
    815.  
    816. function GetMaxAcceleration (grounded : boolean) : float {
    817.     // Maximum acceleration on ground and in air
    818.     if (grounded)
    819.         return movement.maxGroundAcceleration;
    820.     else
    821.         return movement.maxAirAcceleration;
    822. }
    823.  
    824. function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
    825.     // From the jump height and gravity we deduce the upwards speed
    826.     // for the character to reach at the apex.
    827.     return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
    828. }
    829.  
    830. function IsJumping () {
    831.     return jumping.jumping;
    832. }
    833.  
    834. function IsSliding () {
    835.     return (grounded  sliding.enabled  TooSteep());
    836. }
    837.  
    838. function IsTouchingCeiling () {
    839.     return (movement.collisionFlags  CollisionFlags.CollidedAbove) != 0;
    840. }
    841.  
    842. function IsGrounded () {
    843.     return grounded;
    844. }
    845.  
    846. function TooSteep () {
    847.     return (groundNormal.y <= Mathf.Cos(controller.slopeLimit * Mathf.Deg2Rad));
    848. }
    849.  
    850. function GetDirection () {
    851.     return inputMoveDirection;
    852. }
    853.  
    854. function SetControllable (controllable : boolean) {
    855.     canControl = controllable;
    856. }
    857.  
    858. // Project a direction onto elliptical quater segments based on forward, sideways, and backwards speed.
    859. // The function returns the length of the resulting vector.
    860. function MaxSpeedInDirection (desiredMovementDirection : Vector3) : float {
    861.     if (desiredMovementDirection == Vector3.zero)
    862.         return 0;
    863.     else {
    864.         var zAxisEllipseMultiplier : float = (desiredMovementDirection.z > 0 ? movement.maxForwardSpeed : movement.maxBackwardsSpeed) / movement.maxSidewaysSpeed;
    865.         var temp : Vector3 = new Vector3(desiredMovementDirection.x, 0, desiredMovementDirection.z / zAxisEllipseMultiplier).normalized;
    866.         var length : float = new Vector3(temp.x, 0, temp.z * zAxisEllipseMultiplier).magnitude * movement.maxSidewaysSpeed;
    867.         return length;
    868.     }
    869. }
    870.  
    871. function SetVelocity (velocity : Vector3) {
    872.     grounded = false;
    873.     movement.velocity = velocity;
    874.     movement.frameVelocity = Vector3.zero;
    875.     SendMessage("OnExternalVelocity");
    876. }
    877.  
    878. // Require a character controller to be attached to the same game object
    879. @script RequireComponent (CharacterController)
    880. @script AddComponentMenu ("FPS system/Character/FPS Controller")
    881. @script RequireComponent (FPSinput)
    882.  
     
  2. KingKongFu

    KingKongFu

    Joined:
    Sep 18, 2012
    Posts:
    4
    If i got what you want to do right is add the variable fall damage this I take in two different ways
    the amount of damaged received from a fall like this
    var fallDamage : float = 15;
    //say that will take away 15 points from health bar like this
    playerHealth -= fallDamage

    or you want to set the distance which the player falls before taking damages like this
    // Units that player can fall before a falling damage function is run. To disable, type "infinity" in the inspector
    var fallingDamageThreshold = 10.0;

    I hope this helps out
     
  3. exgengames

    exgengames

    Joined:
    Aug 10, 2012
    Posts:
    31
    Nothing y fall not damaged Yet !