Advertisement
Guest User

eqbrowser-animator

a guest
Jun 11th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. /// <summary>
  2. /// CharacterAnimator.cs
  3. /// Character Controller in CSharp v2.3
  4. /// </summary>
  5. using UnityEngine;
  6. using System.Collections;
  7. using EQBrowser;
  8. using CanvasUI;
  9.  
  10. namespace EQBrowser
  11. {
  12. public class CharacterAnimator : MonoBehaviour {
  13.  
  14. //Var definition
  15. public bool swimming = false; //Can be triggert to slow down the movements (like when u swim)
  16. public string moveStatus = "idle"; //movestatus for animations
  17.  
  18. //Movement speeds
  19. private float jumpSpeed = 8.0f; //Jumpspeed / Jumpheight
  20. private float gravity = 20.0f; //Gravity for jump
  21. private float runSpeed = 10.0f; //Speed when the Character is running
  22. private float walkSpeed = 20.0f; //Speed when the Character is walking (normal movement)
  23. private float rotateSpeed = 250.0f; //Rotationspeed of the Character
  24. private float walkBackMod = 0.75f; //Speed in Percent for walk backwards and sidewalk
  25.  
  26. //Internal vars to work with
  27. private float speedMod = 0.0f; //temp Var for Speedcalculation
  28. private bool grounded = false; //temp var if the character is grounded
  29. private Vector3 moveDirection = Vector3.zero; //move direction of the Character
  30. private bool isWalking = false; //toggle var between move and run
  31. private bool jumping = false; //temp var for jumping
  32. private bool CastHealbool = false; //temp var for casting
  33. private bool mouseSideButton = false; //temp var for mouse side buttons
  34. private float pbuffer = 0.0f; //Cooldownpuffer for SideButtons
  35. private float coolDown = 0.5f; //Cooldowntime for SideButtons
  36. private CharacterController controller; //CharacterController for movement
  37. EQBrowser.CharacterAnimator m_avatar;
  38. private Animator anim;
  39. private bool Casting = false;
  40. // public bool CastButton = true;
  41.  
  42.  
  43. void Start ()
  44. {
  45. anim = gameObject.GetComponent<Animator>();
  46. }
  47.  
  48. //Every Frame
  49. void Update ()
  50. {
  51. // Debug.Log (UIScripts.CastButton);
  52. // Debug.Log (Casting);
  53.  
  54. if (this.anim.GetCurrentAnimatorStateInfo (0).IsName ("CastHeal")) {
  55. Casting = true;
  56. moveStatus = "CastHeal";
  57. UIScripts.CastButton = false;
  58. } else {
  59. Casting = false;
  60. }
  61.  
  62. //Set idel animation
  63. isWalking = true;
  64.  
  65. // Hold "Run" to run
  66. if(Input.GetAxis("Run") != 0)
  67. isWalking = false;
  68.  
  69. // Only allow movement and jumps while grounded
  70. if(grounded)
  71. {
  72.  
  73. //movedirection
  74. moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
  75.  
  76. //pushbuffer to avoid on/off flipping
  77. if(pbuffer>0)
  78. pbuffer -=Time.deltaTime;
  79. if(pbuffer<0)pbuffer=0;
  80.  
  81. //Automove Sidebuttonmovement
  82. if((Input.GetAxis("Toggle Move") !=0) && pbuffer == 0){
  83. pbuffer=coolDown;
  84. mouseSideButton = !mouseSideButton;
  85. }
  86.  
  87. //L+R MouseButton Movement
  88. if (Input.GetMouseButton(0) && Input.GetMouseButton(1) || mouseSideButton)
  89. moveDirection.z += 1;
  90. if (moveDirection.z > 1)
  91. moveDirection.z = 1;
  92.  
  93. //Strafing move (like Q/E movement
  94. moveDirection.x -= Input.GetAxis("Strafing");
  95.  
  96. // if moving forward and to the side at the same time, compensate for distance
  97. if(Input.GetMouseButton(1) && (Input.GetAxis("Horizontal") != 0) && (Input.GetAxis("Vertical") != 0))
  98. {
  99. moveDirection *= 0.7f;
  100. }
  101.  
  102. //Speedmodification / is moving forward or side/backward
  103. speedMod = ((Input.GetAxis("Vertical") < 0) || (Input.GetMouseButton(1) && (Input.GetAxis("Horizontal")) != 0) || Input.GetAxis("Strafing") != 0) ? walkBackMod : 1.0f;
  104.  
  105. //Use run or walkspeed
  106. moveDirection *= isWalking ? walkSpeed * speedMod : runSpeed * speedMod;
  107.  
  108. //reduce movement by 70% when swimming is toggled
  109. moveDirection*= swimming ? 0.7f : 1;
  110.  
  111. // Jump!
  112. if(Input.GetButton("Jump") && (Casting == false))
  113. {
  114. jumping = true;
  115. moveDirection.y = jumpSpeed;
  116. GetComponent<Animator>().Play("Jump");
  117. }
  118.  
  119.  
  120. var turningright = (Input.GetAxis("Horizontal") > 0);
  121. var turningleft = (Input.GetAxis("Horizontal") < 0);
  122.  
  123. if ((turningright == true) && (moveDirection.z == 0) && (Casting == false))
  124. {
  125. moveStatus = "Turn";
  126. GetComponent<Animator>().Play("Turn");
  127. }
  128. if ((turningleft == true) && (moveDirection.z == 0) && (Casting == false))
  129. {
  130. moveStatus = "Turn";
  131. GetComponent<Animator>().Play("Turn");
  132. }
  133. //movestatus normal movement (for animations)
  134. if ((moveDirection.x == 0) && (moveDirection.z == 0) && (turningright == false) && (turningleft == false))
  135. {
  136. if((UIScripts.CastButton == true) && (moveStatus == "idle"))
  137. {
  138. GetComponent<Animator>().Play("CastHeal");
  139. UIScripts.CastButton = false;
  140. }
  141. // if(Casting == false && UIScripts.CastButton == false)
  142. if(Casting == false && UIScripts.CastButton == false)
  143. {
  144. moveStatus = "idle";
  145. GetComponent<Animator>().Play("Idle");
  146. }
  147. }
  148. if(Casting == false)
  149. {
  150. if (moveDirection.z > 0)
  151. {
  152. UIScripts.CastButton = false;
  153. moveStatus = isWalking ? "walking" : "running";
  154. GetComponent<Animator>().Play("Walk");
  155. }
  156. if (moveDirection.z < 0)
  157. {
  158. UIScripts.CastButton = false;
  159. moveStatus = isWalking ? "backwalking" : "backrunning";
  160. GetComponent<Animator>().Play("Walk");
  161. }
  162. if (moveDirection.x > 0)
  163. {
  164. UIScripts.CastButton = false;
  165. moveStatus = isWalking ? "sidewalking_r" : "siderunning_r";
  166. GetComponent<Animator>().Play("Walk");
  167. }
  168. if (moveDirection.x < 0)
  169. {
  170. UIScripts.CastButton = false;
  171. moveStatus = isWalking ? "sidewalking_l" : "siderunning_l";
  172. GetComponent<Animator>().Play("Walk");
  173. }
  174. }
  175. //movestatus swim movement (for animations)
  176. if(swimming)
  177. {
  178. if ((moveDirection.x == 0) && (moveDirection.z == 0))
  179. {
  180. moveStatus = "swimidle";
  181. }
  182. if (moveDirection.z > 0)
  183. {
  184. moveStatus = isWalking ? "swim" : "swimfast";
  185. }
  186. if (moveDirection.z < 0)
  187. {
  188. moveStatus = isWalking ? "backswim" : "backswimfast";
  189. }
  190. if (moveDirection.x > 0)
  191. {
  192. moveStatus = isWalking ? "sideswim_r" : "sideswimfast_r";
  193. }
  194. if (moveDirection.x < 0)
  195. {
  196. moveStatus = isWalking ? "sidewswim_l" : "sideswimfast_l";
  197. }
  198. if (jumping)
  199. {
  200. moveStatus = "swimup";
  201. }
  202. }
  203.  
  204. //transform direction
  205. moveDirection = transform.TransformDirection(moveDirection);
  206.  
  207. }
  208. // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
  209. if(Input.GetMouseButton(1)) {
  210. transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
  211. }
  212. else
  213. {
  214. transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
  215. }
  216.  
  217. //Apply gravity
  218. moveDirection.y -= gravity * Time.deltaTime;
  219.  
  220. //Get CharacterController
  221. controller = GetComponent<CharacterController>();
  222. //Move Charactercontroller and check if grounded
  223. grounded = ((controller.Move(moveDirection * Time.deltaTime)) & CollisionFlags.Below) != 0;
  224.  
  225. //Reset jumping after landing
  226. jumping = grounded ? false : jumping;
  227.  
  228. //movestatus jump/swimup (for animations)
  229. if(jumping)
  230. moveStatus = "jump";
  231. if(jumping && swimming)
  232. moveStatus = "swimup";
  233. }
  234.  
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement