Advertisement
Guest User

Untitled

a guest
Apr 13th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MovementClass : MonoBehaviour
  5. {
  6. [HideInInspector]
  7. public enum facing { Right, Left }
  8. [HideInInspector]
  9. public facing facingDir;
  10.  
  11. [HideInInspector]
  12. public enum moving { Right, Left, None }
  13. [HideInInspector]
  14. public moving movingDir;
  15.  
  16. [HideInInspector]
  17. public bool isLeft;
  18. [HideInInspector]
  19. public bool isRight;
  20. [HideInInspector]
  21. public bool isJump;
  22.  
  23. [HideInInspector]
  24. public bool jumping = false;
  25. [HideInInspector]
  26. public bool grounded = false;
  27. // Know when to play landing animation.
  28. bool prevGrounded = false;
  29.  
  30. [HideInInspector]
  31. public bool blockedRight;
  32. [HideInInspector]
  33. public bool blockedLeft;
  34. [HideInInspector]
  35. public bool blockedUp;
  36. [HideInInspector]
  37. public bool blockedDown;
  38.  
  39. [HideInInspector]
  40. public bool alive = true;
  41. [HideInInspector]
  42. public Vector3 spawnPos;
  43.  
  44. protected Transform thisTransform;
  45.  
  46. private float moveVel;
  47. private float runVel = 3f;
  48. private float walkVel = 2f; // While carrying object
  49. private Vector3 vel2;
  50. private Vector3 vel;
  51.  
  52. private float jumpVel = 14f;
  53. private float jump2Vel = 8f;
  54. private float fallVel = 16f;
  55.  
  56. private int jumps = 0;
  57. private int maxJumps = 1; // set to 2 for double jump
  58.  
  59. private float gravityY;// = 52f;
  60. private float maxVelY = 0f;
  61.  
  62. private RaycastHit hitInfo;
  63. private float halfMyX = 0.325f; //0.25f;
  64. private float halfMyY = 0.5f;//0.375f;
  65. [HideInInspector]
  66. public float rayDistUp = 0.375f;
  67.  
  68. private float absVel2X;
  69. private float absVel2Y;
  70.  
  71. // layer masks
  72. protected int groundMask = 1 << 9 | 1 << 8; // Ground, Block
  73. protected int blockMask = 1 << 8; //Block
  74.  
  75. public virtual void Awake()
  76. {
  77. thisTransform = transform;
  78. }
  79.  
  80. // Use this for initialization
  81. public virtual void Start()
  82. {
  83. moveVel = runVel;
  84. maxVelY = fallVel;
  85. vel.y = 0;
  86. StartCoroutine(StartGravity());
  87. }
  88.  
  89. IEnumerator StartGravity()
  90. {
  91. // wait for things to settle before applying gravity
  92. yield return new WaitForSeconds(0.1f);
  93. gravityY = 52f;
  94. }
  95.  
  96. // Update is called once per frame
  97. public virtual void UpdateMovement()
  98. {
  99. vel.x = 0;
  100.  
  101. if (!grounded)
  102. {
  103. jumps = 1;
  104. }
  105. else
  106. {
  107. jumps = 0;
  108. }
  109.  
  110. // pressed right button
  111. if (isRight == true)
  112. {
  113. vel.x = moveVel;
  114. }
  115.  
  116. // pressed left button
  117. if (isLeft == true)
  118. {
  119. vel.x = -moveVel;
  120. }
  121.  
  122. // pressed jump button
  123. if (isJump == true)
  124. {
  125. if (jumps < maxJumps)
  126. {
  127. jumps += 1;
  128. jumping = true;
  129. if (jumps == 1)
  130. {
  131. vel.y = jumpVel;
  132. }
  133. if (jumps == 2)
  134. {
  135. vel.y = jump2Vel;
  136. }
  137. }
  138. }
  139.  
  140. // landed from fall/jump
  141. if (grounded == true && vel.y == 0)
  142. {
  143. jumping = false;
  144. jumps = 0;
  145. }
  146.  
  147. UpdateRaycasts();
  148.  
  149. // apply gravity while airborne
  150. if (grounded == false)
  151. {
  152. vel.y -= gravityY * Time.deltaTime;
  153. }
  154.  
  155. // velocity limiter
  156. if (vel.y < -maxVelY)
  157. {
  158. vel.y = -maxVelY;
  159. }
  160.  
  161. // apply movement
  162. vel2 = vel * Time.deltaTime;
  163. thisTransform.position += new Vector3(vel2.x, vel2.y, 0f);
  164. }
  165.  
  166. // ============================== RAYCASTS ==============================
  167.  
  168. void UpdateRaycasts()
  169. {
  170. blockedRight = false;
  171. blockedLeft = false;
  172. blockedUp = false;
  173. blockedDown = false;
  174. grounded = false;
  175.  
  176. absVel2X = Mathf.Abs(vel2.x);
  177. absVel2Y = Mathf.Abs(vel2.y);
  178.  
  179. if (Physics.Raycast(new Vector3(thisTransform.position.x - 0.25f, thisTransform.position.y, thisTransform.position.z), -Vector3.up, out hitInfo, 0.6f + absVel2Y, groundMask)
  180. || Physics.Raycast(new Vector3(thisTransform.position.x + 0.25f, thisTransform.position.y, thisTransform.position.z), -Vector3.up, out hitInfo, 0.6f + absVel2Y, groundMask))
  181. {
  182. // not while jumping so he can pass up thru platforms
  183. if (vel.y <= 0)
  184. {
  185. grounded = true;
  186. vel.y = 0f; // stop falling
  187. thisTransform.position = new Vector3(thisTransform.position.x, hitInfo.point.y + halfMyY, 0f);
  188. }
  189. }
  190.  
  191. // blocked up
  192. if (Physics.Raycast(new Vector3(thisTransform.position.x - 0.2f, thisTransform.position.y, thisTransform.position.z), Vector3.up, out hitInfo, rayDistUp + absVel2Y, groundMask)
  193. || Physics.Raycast(new Vector3(thisTransform.position.x + 0.2f, thisTransform.position.y, thisTransform.position.z), Vector3.up, out hitInfo, rayDistUp + absVel2Y, groundMask))
  194. {
  195. BlockedUp();
  196. }
  197.  
  198. // blocked on right
  199. if (Physics.Raycast(new Vector3(thisTransform.position.x, thisTransform.position.y, thisTransform.position.z), Vector3.right, out hitInfo, halfMyX + absVel2X, groundMask))
  200. {
  201. BlockedRight();
  202. }
  203.  
  204. // blocked on left
  205. if (Physics.Raycast(new Vector3(thisTransform.position.x, thisTransform.position.y, thisTransform.position.z), -Vector3.right, out hitInfo, halfMyX + absVel2X, groundMask))
  206. {
  207. BlockedLeft();
  208. }
  209. }
  210.  
  211. void BlockedUp()
  212. {
  213. if (vel.y > 0)
  214. {
  215. vel.y = 0f;
  216. blockedUp = true;
  217. }
  218. }
  219.  
  220. void BlockedRight()
  221. {
  222. if (facingDir == facing.Right || movingDir == moving.Right)
  223. {
  224. blockedRight = true;
  225. vel.x = 0f;
  226. thisTransform.position = new Vector3(hitInfo.point.x - (halfMyX - 0.01f), thisTransform.position.y, 0f); // .01 less than collision width.
  227. }
  228. }
  229.  
  230. void BlockedLeft()
  231. {
  232. if (facingDir == facing.Left || movingDir == moving.Left)
  233. {
  234. blockedLeft = true;
  235. vel.x = 0f;
  236. thisTransform.position = new Vector3(hitInfo.point.x + (halfMyX - 0.01f), thisTransform.position.y, 0f); // .01 less than collision width.
  237. }
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement