Guest User

Untitled

a guest
Jun 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharacterGravity : MonoBehaviour
  6. {
  7. private Rigidbody rb;
  8. public bool gravityOn;
  9. private float avoidGravity = 9.81f * 2.0f * 1.0f; //To transform gravity to its inverse.
  10. private float normalGravity = 0.0f; //To get the normal gravity.
  11. private Vector3 floor; //It'll set the distance until the point just under you.
  12. private Vector3 roof; //It'll set the distance until the point just above you.
  13. public GameObject onRoof; //Empty GameObject with the xyz reference which will have the player on roof.
  14. public GameObject onFloor; //Empty GameObject with the xyz reference which will have the player on floor.
  15. public GameObject turnReference; //It takes values from onRoof and onFloor depending on the gravity values.
  16. private float distanceTo; //It calculates everytime the distance to the surface which is not being walked by the player.
  17. private float distanceToValor; //It sets the value from distanceTo when the change of gravity is activated.
  18. private float gradesPerSecond; //It calculates the grades that the player has to rotate per second to land standing on the surface.
  19.  
  20. // Use this for initialization
  21. void Start()
  22. {
  23. rb = GetComponent<Rigidbody>();
  24. gravityOn = true;
  25. referenciaGiro = onFloor;
  26. }
  27.  
  28. // Update is called once per frame
  29. void Update()
  30. {
  31.  
  32. }
  33.  
  34. private void FixedUpdate()
  35. {
  36. Gravity();
  37. LerpRoof();
  38. }
  39.  
  40. void Gravity() //Change values of the gravity force which affetcs the player.
  41. {
  42. if (Input.GetKeyDown(KeyCode.C))
  43. {
  44. gravityOn = !gravityOn;
  45. distanceToValor = distanceTo;
  46. }
  47.  
  48. if (gravityOn)
  49. {
  50. rb.AddForce(new Vector3(0, normalGravity, 0));
  51. }
  52. else
  53. {
  54. rb.AddForce(new Vector3(0, avoidGravity, 0));
  55. }
  56.  
  57.  
  58. }
  59.  
  60. void LerpRoof()
  61. {
  62. if (gravityOn)
  63. {
  64.  
  65. RaycastHit hit;
  66. // Roof detection.
  67. Ray upRay = new Ray (transform.position, Vector3.up);
  68.  
  69.  
  70. if (Physics.Raycast( upRay, out hit))
  71. {
  72. Debug.Log("Collider es " + hit.collider.name);
  73. roof = hit.point;
  74. distanceTo = Vector3.Distance(transform.position, hit.point);
  75. }
  76. else
  77. {
  78.  
  79. }
  80.  
  81. }
  82. else
  83. {
  84. RaycastHit hit;
  85.  
  86. // Floor detection.
  87. Ray downRay = new Ray(transform.position, Vector3.down);
  88.  
  89.  
  90. if (Physics.Raycast(downRay, out hit))
  91. {
  92. floor = hit.point;
  93. distanceTo = Vector3.Distance(transform.position, floor);
  94. }
  95. else
  96. {
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. }
  105.  
  106. //Change of rotation reference to roof/floor.
  107. gradesPerSecond = (180/ (((2.0f * distanceToValor) / 3.0f) / Physics.gravity.magnitude));
  108.  
  109. if (Input.GetKeyDown(KeyCode.C) && gravityOn == true)
  110. {
  111. turnReference = onFloor;
  112. }
  113. if (Input.GetKeyDown(KeyCode.C) && gravityOn == false)
  114. {
  115. turnReference = onRoof;
  116. }
  117.  
  118. transform.rotation = Quaternion.RotateTowards(transform.rotation, turnReference.transform.rotation, gradesPerSecond * Time.deltaTime);
  119.  
  120. }
  121. }
Add Comment
Please, Sign In to add comment