Advertisement
Guest User

Untitled

a guest
May 28th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player : MonoBehaviour {
  5. public float moveSpeed = 20f;
  6. public float maxSpeed = 20f;
  7. public float friction = 1f;
  8. public Vector3 frict;
  9. public Transform[] debug;
  10. public float ts = 1f;
  11.  
  12. public bool Grounded = false;
  13. private BoxCollider2D boxcc = null;
  14. private Vector3 lastpos = Vector3.zero;
  15. public Vector3 Velocity = Vector3.zero;
  16.  
  17. private LineRenderer lr;
  18. private float haxis = 0f;
  19. private float SpeedScale = 100f; //Could have better name?
  20.  
  21. public static Player Instance; //temp singleton
  22. public Vector3 feet = Vector3.zero;
  23. // Use this for initialization
  24. void Awake () {
  25. Instance = this;
  26. boxcc = GetComponent<BoxCollider2D>();
  27. lr = GetComponent<LineRenderer>();
  28. }
  29. void AddVelocity(Vector3 vel)
  30. {
  31. Velocity += vel / SpeedScale;
  32. }
  33. void AddVelocity(float x, float y, float z)
  34. {
  35. Velocity.x += x / SpeedScale;
  36. Velocity.y += y / SpeedScale;
  37. Velocity.z += z / SpeedScale;
  38. }
  39. Vector3 GetVelocity()
  40. {
  41. return Velocity * SpeedScale;
  42. }
  43. void Land(Vector3 r)
  44. {
  45. Vector3 ground = r;
  46. ground.y += boxcc.bounds.extents.y;
  47. Grounded = true;
  48. transform.Translate(0f, ground.y - transform.position.y, 0f);
  49. //AddVelocity (0f, ground.y - transform.position.y, 0f);
  50. Velocity.y = 0f;
  51. lr.SetPosition (1, r);
  52. }
  53. void FixedUpdate()
  54. {
  55. Vector2 lastposcheck = lastpos;
  56. lastposcheck.y += boxcc.bounds.extents.y;
  57. Vector2 poscheck = transform.position;
  58. poscheck.y -= boxcc.bounds.extents.y;
  59. feet = poscheck;
  60. float chkdistance = 0.8f;
  61. //
  62. //If we're falling, check for ground
  63. //
  64. if (GetVelocity ().y < 0f && !Grounded)
  65. {
  66. RaycastHit2D[] ray = Physics2D.RaycastAll(poscheck, Velocity, Vector2.Distance (transform.position, transform.position + Velocity));
  67. foreach (RaycastHit2D r in ray)
  68. {
  69. if (r.collider != boxcc)
  70. {
  71. Land (r.point);
  72. Debug.Log ("Landed ray");
  73. }
  74. }
  75. }
  76.  
  77. //
  78. //If we aren't moving upwards, check that we are still on the ground
  79. //Also check for ground in a box below us
  80. //
  81. if (GetVelocity ().y <= 0f)
  82. {
  83.  
  84. Vector2 poscheckbox = transform.position;
  85. //poscheckbox.y -= chkdistance / 2;
  86. Vector2 bxchkextents = boxcc.bounds.size;
  87. bxchkextents.y /= 1.1f;
  88. bxchkextents.x /= 1.1f;
  89. RaycastHit2D groundcheckray = Physics2D.BoxCast(poscheckbox, bxchkextents, 0f, -Vector2.up, chkdistance);
  90. if (groundcheckray.collider == null)
  91. {
  92. Grounded = false;
  93. }
  94. else
  95. {
  96. if ((poscheck.y + Velocity.y) > groundcheckray.point.y)
  97. {
  98. Grounded = false;
  99. }
  100. if (!Grounded && (poscheck.y + Velocity.y) < groundcheckray.point.y)
  101. {
  102. Land (groundcheckray.point);
  103. //Debug
  104. debug[0].position = groundcheckray.point;
  105. Debug.Log ("Landed box");
  106.  
  107. }
  108. lr.SetPosition (1, groundcheckray.point);
  109. debug[1].position = groundcheckray.point;
  110. }
  111.  
  112. }
  113. //Gravity
  114. if (!Grounded)
  115. AddVelocity (Physics2D.gravity * Time.deltaTime);
  116.  
  117. //Horizontal movement
  118. if (GetVelocity ().x < (maxSpeed * haxis) && haxis > 0f)
  119. AddVelocity ( Vector3.right * Mathf.Min ( haxis * moveSpeed * Time.deltaTime, (maxSpeed - GetVelocity ().x) * haxis));
  120. if (GetVelocity ().x > (maxSpeed * haxis) && haxis < 0f)
  121. AddVelocity ( Vector3.right * Mathf.Max ( haxis * moveSpeed * Time.deltaTime, (-maxSpeed - GetVelocity ().x) * -haxis));
  122.  
  123.  
  124.  
  125. //Friction
  126. frict = Vector3.zero;
  127. float temphaxis = (haxis < 0f ? -haxis : haxis);
  128. temphaxis = (temphaxis / -1) + 1;
  129. if ((GetVelocity ().x > haxis * maxSpeed || GetVelocity().x < haxis * maxSpeed) && GetVelocity ().x != 0f)
  130. frict = Vector3.right * ((GetVelocity ().x > 0f ? Mathf.Max (-friction, -GetVelocity ().x) : Mathf.Min (friction, -GetVelocity().x) )) * temphaxis;
  131. AddVelocity (frict);
  132.  
  133. //
  134. //Check horizontal collision
  135. //
  136. if (Velocity.x != 0f)
  137. {
  138. RaycastHit2D horray;
  139. Vector2 sideboxcheckbounds = boxcc.bounds.size;
  140. sideboxcheckbounds.y /= 1.1f;
  141. sideboxcheckbounds.x /= 1.1f;
  142. horray = Physics2D.BoxCast(transform.position, sideboxcheckbounds, 0f, Velocity.x > 0 ? Vector2.right : -Vector2.right, chkdistance);
  143. if (horray.collider != null)
  144. {
  145. if ((Velocity.x < 0f && (transform.position.x - sideboxcheckbounds.x + Velocity.x) < horray.point.x)
  146. || (Velocity.x > 0f && (transform.position.x + sideboxcheckbounds.x + Velocity.x) > horray.point.x))
  147. {
  148. SnapToWall(horray.point);
  149. Debug.Log ("Snapped");
  150.  
  151. }
  152. debug[2].position = horray.point;
  153. }
  154. }
  155.  
  156. //
  157. //Check collision above yourself
  158. //
  159. if (Velocity.y > 0f)
  160. {
  161. RaycastHit2D aboveray;
  162. Vector2 aboveboxbounds = boxcc.bounds.size;
  163. aboveboxbounds.y /= 1.1f;
  164. aboveboxbounds.x /= 1.1f;
  165. aboveray = Physics2D.BoxCast (transform.position, aboveboxbounds, 0f, Vector2.up, chkdistance);
  166. if (aboveray.collider != null)
  167. {
  168. if (aboveray.point.y < (transform.position.y + boxcc.bounds.extents.y + Velocity.y))
  169. {
  170. Debug.Log (aboveray.point.y + " : " + (transform.position.y + boxcc.bounds.extents.y + Velocity.y));
  171. SnapToCeiling(aboveray.point);
  172. Debug.Log ("Ceiling snapped");
  173.  
  174.  
  175. }
  176. debug[3].position = aboveray.point;
  177. }
  178. }
  179.  
  180. //Apply velocity
  181. transform.position += Velocity;
  182. lastpos = transform.position;
  183.  
  184. //Debug
  185. lr.SetPosition(0, transform.position);
  186.  
  187.  
  188. }
  189.  
  190. void SnapToCeiling(Vector3 r)
  191. {
  192. Vector3 ceiling = r;
  193. ceiling.y = ceiling.y - boxcc.bounds.extents.y;
  194. transform.Translate (0f, ceiling.y - transform.position.y, 0f);
  195. Velocity.y = 0f;
  196. }
  197. void SnapToWall(Vector3 r)
  198. {
  199. Vector3 wall = r;
  200. wall.x += wall.x < transform.position.x ? boxcc.bounds.extents.x : -boxcc.bounds.extents.x;
  201. transform.Translate (wall.x - transform.position.x, 0f, 0f);
  202. Velocity.x = 0f;
  203.  
  204. }
  205. // Update is called once per frame
  206. void Update () {
  207.  
  208. Time.timeScale = ts;
  209.  
  210.  
  211. if (Input.GetButtonDown("Fire1") && Grounded)
  212. {
  213. AddVelocity (Vector3.up * 10f);
  214. Grounded = false;
  215. }
  216.  
  217. haxis = Input.GetAxisRaw("Horizontal");
  218.  
  219.  
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement