Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

portal activates fall damage

Discussion in 'Editor & General Support' started by gauty40, Sep 1, 2014.

  1. gauty40

    gauty40

    Joined:
    Feb 22, 2014
    Posts:
    15
    if anyone can help me with the most effiecent way to make it so my portal doesnt activate fall damage (i was thinking adding a "no-fall damage" tag to it) it would be appreciated as i haven't a clue on how to fix it, rigid controller script attached

    Code (csharp):
    1.  
    2. varcrouchSpeed = 2.0;
    3. varwalkSpeed = 8.0;
    4. varrunSpeed = 10.0;
    5.  
    6. varfallDamageMultiplier : int = 2;
    7. varfallAnimGO : GameObject;
    8. varinAirControl = 0.1;
    9. vargravity = 25.0;
    10. varNormalGravity:float;
    11. varmaxVelocityChange = 10.0;
    12. varcanJump = true;
    13. varjumpHeight = 1.5;
    14. varNormaljumpHeight:float;
    15. varfallSound : AudioClip;
    16. varplayerWeapons : GameObject;
    17. varplayeranimation : PlayerAnimation;
    18. //@HideInInspector
    19. vargrounded = false;
    20. privatevarsliding : boolean = false;
    21. privatevarspeed = 10.0;
    22. privatevarlimitDiagonalSpeed = true;
    23. privatevarcrouching : boolean;
    24. privatevarnormalHeight : float = 0.5;
    25. privatevarcrouchHeight : float = -0.2;
    26. privatevarcrouchingHeight = 0.3;
    27. privatevarhit : RaycastHit;
    28. privatevarmyTransform : Transform;
    29. privatevarrayDistance : float;
    30. privatevarmainCameraGO : GameObject;
    31. privatevarweaponCameraGO : GameObject;
    32.  
    33. @scriptRequireComponent(Rigidbody, CapsuleCollider)
    34.  
    35. functionStart(){
    36. gravity=NormalGravity;
    37. jumpHeight=NormaljumpHeight;
    38. }
    39.  
    40. functionAwake (){
    41. rigidbody.freezeRotation = true;
    42. rigidbody.useGravity = false;
    43. myTransform = transform;
    44. mainCameraGO = gameObject.FindWithTag("MainCamera");
    45. weaponCameraGO = gameObject.FindWithTag("WeaponCamera");
    46. rayDistance = collider.height * .5 + collider.radius;
    47. playerWeapons.animation.wrapMode = WrapMode.Loop;
    48. }
    49.  
    50. functionFixedUpdate (){
    51. if (grounded){
    52. varinputX = Input.GetAxis("Horizontal");
    53. varinputY = Input.GetAxis("Vertical");
    54. varinputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
    55.  
    56. if (Physics.Raycast(myTransform.position, -Vector3.up, hit, rayDistance)) {
    57. if (Vector3.Angle(hit.normal, Vector3.up) > 30){
    58. sliding = true;
    59. rigidbody.AddRelativeForce (-Vector3.up * 500);
    60.  }else{
    61. sliding = false;
    62.  
    63.  }
    64.  }
    65.  
    66. //Calculatehowfastweshouldbemoving
    67. vartargetVelocity = newVector3(inputX * inputModifyFactor, 0.0, inputY * inputModifyFactor);
    68. targetVelocity = myTransform.TransformDirection(targetVelocity);
    69. targetVelocity *= speed;
    70.  
    71. //Applyaforcethatattemptstoreachourtargetvelocity
    72. varvelocity = rigidbody.velocity;
    73. varvelocityChange = (targetVelocity - velocity);
    74. velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    75. velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    76. velocityChange.y = 0;
    77. rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    78.  
    79.  
    80. if (canJump && Input.GetButton("Jump")){
    81. rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    82.  }
    83.  
    84. if(!crouching){
    85. if (grounded && Input.GetButton("Run") && Input.GetKey("w"))
    86. speed = runSpeed;
    87. else
    88. speed = walkSpeed;
    89.  }else{
    90. speed = crouchSpeed;
    91.  }
    92.  
    93.  }else{
    94.  
    95. //AirControl
    96. targetVelocity = newVector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    97. targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;
    98. rigidbody.AddForce(targetVelocity, ForceMode.VelocityChange);
    99.  }
    100.  
    101. //Gravity
    102. rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
    103. grounded = false;
    104. }
    105.  
    106. functionOnCollisionStay (col : Collision){
    107.  
    108. for(varcontact : ContactPointincol.contacts){
    109. if(Vector3.Angle(contact.normal, Vector3.up) < 45){
    110. grounded = true;
    111.  }
    112.  }
    113. }
    114.  
    115. functionHitJumpPad(velocity: float) {
    116. rigidbody.velocity.z += velocity;
    117.  }
    118.  
    119. functionOnCollisionEnter (collision : Collision){
    120. if(!grounded){
    121. fallAnimGO.animation.CrossFadeQueued("Fall", 0.3, QueueMode.PlayNow);
    122. varcurrSpeed : float = collision.relativeVelocity.magnitude;
    123.  
    124. if (currSpeed > 25) {
    125. vardamage : float = currSpeed * fallDamageMultiplier;
    126. Debug.Log ("FallDamage" + damage);
    127. SendMessage ("PlayerDamage", damage, SendMessageOptions.DontRequireReceiver);
    128.  }
    129.  }
    130. }
    131.  
    132. functionCalculateJumpVerticalSpeed (){
    133. returnMathf.Sqrt(2 * jumpHeight * gravity);
    134. }
    135.  
    136. functionUpdate(){
    137. if(grounded){
    138. playeranimation.ground = true;
    139. }else{
    140. playeranimation.ground = false;
    141. }
    142.  
    143. if (grounded && rigidbody.velocity.magnitude < (walkSpeed+2) && rigidbody.velocity.magnitude > (walkSpeed-2) && !Input.GetButton("Fire2")){
    144. playerWeapons.animation.CrossFade("Walk");
    145.  
    146.  }elseif (grounded && rigidbody.velocity.magnitude < (runSpeed+2) && rigidbody.velocity.magnitude > (runSpeed -2)){
    147. playerWeapons.animation.CrossFade("Run");
    148.  
    149.  }elseif (grounded && rigidbody.velocity.magnitude < (crouchSpeed+1) && rigidbody.velocity.magnitude > (crouchSpeed-1) && Input.GetButton("Fire2"))
    150. playerWeapons.animation.CrossFade("CrouchAim");
    151.  
    152. else
    153. playerWeapons.animation.CrossFade("IdleAnim");
    154.  
    155.  
    156. if(mainCameraGO.transform.localPosition.y > normalHeight){
    157. mainCameraGO.transform.localPosition.y = normalHeight;
    158.  } elseif(mainCameraGO.transform.localPosition.y < crouchHeight){
    159. mainCameraGO.transform.localPosition.y = crouchHeight;
    160.  }
    161.  
    162. if(weaponCameraGO.transform.localPosition.y > normalHeight){
    163. weaponCameraGO.transform.localPosition.y = normalHeight;
    164.  } elseif(weaponCameraGO.transform.localPosition.y < crouchHeight){
    165. weaponCameraGO.transform.localPosition.y = crouchHeight;
    166.  }
    167.  
    168. if (Input.GetButtonDown("Crouch") && !crouching) {
    169. collider.height = 1.5;
    170. collider.center = Vector3 (0, -0.25, 0);
    171. crouching = true;
    172.  }
    173.  
    174. if(Input.GetButtonUp("Crouch") && crouching){
    175. collider.height = 2.0;
    176. collider.center = Vector3 (0, 0, 0);
    177. crouching = false;
    178.  }
    179.  
    180. if(crouching){
    181. if(mainCameraGO.transform.localPosition.y > crouchHeight){
    182. if(mainCameraGO.transform.localPosition.y - (crouchingHeight * Time.deltaTime/.1) < crouchHeight){
    183. mainCameraGO.transform.localPosition.y = crouchHeight;
    184.  } else {
    185. mainCameraGO.transform.localPosition.y -= crouchingHeight * Time.deltaTime/.1;
    186.  }
    187.  }
    188. if(weaponCameraGO.transform.localPosition.y > crouchHeight){
    189. if(weaponCameraGO.transform.localPosition.y - (crouchingHeight * Time.deltaTime/.1) < crouchHeight){
    190. weaponCameraGO.transform.localPosition.y = crouchHeight;
    191.  } else {
    192. weaponCameraGO.transform.localPosition.y -= crouchingHeight * Time.deltaTime/.1;
    193.  }
    194.  }
    195.  
    196.  } else {
    197. if(mainCameraGO.transform.localPosition.y < normalHeight){
    198. if(mainCameraGO.transform.localPosition.y + (crouchingHeight * Time.deltaTime/.1) > normalHeight){
    199. mainCameraGO.transform.localPosition.y = normalHeight;
    200.  } else {
    201. mainCameraGO.transform.localPosition.y += crouchingHeight * Time.deltaTime/.1;
    202.  }
    203.  }
    204. if(weaponCameraGO.transform.localPosition.y < normalHeight){
    205. if(weaponCameraGO.transform.localPosition.y + (crouchingHeight * Time.deltaTime/.1) > normalHeight){
    206. weaponCameraGO.transform.localPosition.y = normalHeight;
    207.  } else {
    208. weaponCameraGO.transform.localPosition.y += crouchingHeight * Time.deltaTime/.1;
    209.  }
    210.  }
    211.  }
    212. }
    213.  
    214. functionAccelerate (accelerateY : float, accelerateZ : float){
    215. grounded = false;
    216. rigidbody.AddRelativeForce (0, accelerateY, accelerateZ);
    217. }
    218. [CODE/]
     
  2. gauty40

    gauty40

    Joined:
    Feb 22, 2014
    Posts:
    15
    annnnnnyyyyyyyyooooonnnneeee!?
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Sorry, I'd love to help, but I haven't a clue what you're asking.
     
  4. gauty40

    gauty40

    Joined:
    Feb 22, 2014
    Posts:
    15
    I have a portal system setup in my game & a fall damage system, when the player is moving at a certain velocity and the stops suddenly the player takes damage, the portals are (most likely) slowing the player down too much and causing the damage affect, I have no clue on how to fix this
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, here's one thought... change your fall-damage script so that it only applies damage if the player's velocity in y (in particular) suddenly decreases. Assuming the player tends to be moving through portals in mostly X and Z, then they won't affect the y velocity, so the fall damage won't apply.

    Another thought is to rework the fall-damage script more extensively, so that it relies on collision events rather than changes in velocity. The general logic would be: on collision, see if what you're colliding with is something solid (like the ground), and if so, take damage proportional to your velocity towards the collision point. This can be a bit tricky to get right since you're probably colliding with the ground all the time, but with tweaking it can certainly be made to work.

    A third thought is to figure out why the portals are slowing the player down at all, and fix that.

    HTH,
    - Joe