Advertisement
Guest User

Untitled

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