Advertisement
Guest User

The Code

a guest
Jan 28th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.14 KB | None | 0 0
  1. /*
  2. * Player Input Script(Using New Input System)
  3. * Resource: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Installation.html
  4. * Created by: Bradley Williamson
  5. * On: 1/7/20
  6. */
  7.  
  8. using Complete;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.InputSystem;
  13.  
  14. public class CharacterManager : MonoBehaviour
  15. {
  16. // Public Variables
  17. public enum CharacterClass { NONE, Paladin, Warrior, Assassin, Archer };
  18.  
  19.  
  20. [Header("Character Class")]
  21. public CharacterClass characterClass;
  22.  
  23.  
  24.  
  25. [Header("Movement")]
  26. public float speed = 4.5f; // Original = 4.5f
  27. public float rotSpeed = 10.0f;
  28.  
  29. [Header("Camera")]
  30. public Camera mainCamera;
  31.  
  32. [Header("CycleInputTimer")]
  33. public float cycleSpeed = 0.5f;
  34. public float cycleRange = 10f;
  35.  
  36. [Header("Right Analog Stick Targeting Range")]
  37. public float analogTargetRange = 5.0f;
  38.  
  39. // Private Variables
  40. PlayerInputActions inputAction; // InputActions
  41. Rigidbody playerRBody;
  42. //Character Input variables
  43. Vector2 rotationDirection, movementInput;
  44. Vector3 inputDirection, inputRotation;
  45. float xMove, yMove, xRot, yRot, cycleTimer;
  46. //Camera information
  47. Vector3 camForward, camRight;
  48. GameObject targetedEnemy;
  49. Quaternion newRotation;
  50. // Character Abilities/scripts
  51. BasicAttack basicAttack;
  52. //Paladin
  53. paladinHealingSpring paladinEarthHealingSpring;
  54. Taunt paladinTaunt;
  55. //Warrior
  56. AxeWhirlwind warriorAxeWhirlwind;
  57. FlamingLeap warriorFalmingLeap;
  58.  
  59. //Assassin
  60. ThunderStrike thunderStrike;
  61. Execution execution;
  62.  
  63. //Archer
  64. ArrowVolley arrowVolley;
  65. PiercingArrow piercingArrow;
  66.  
  67. //Player and enemy layer index
  68. int playerLayerIndex, enemyLayerIndex;
  69.  
  70. private void Awake()
  71. {
  72. inputAction = new PlayerInputActions();// Generate new PlayerInputActions
  73. //Using the input performed method to retrieve the input value and assign to the new created variables in the fixed update
  74. inputAction.PlayerControls.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
  75. inputAction.PlayerControls.RotateCharacter.performed += ctx => rotationDirection = ctx.ReadValue<Vector2>();
  76.  
  77.  
  78. //Get the player and enemy layermask id's
  79. playerLayerIndex = LayerMask.NameToLayer("Player");
  80. enemyLayerIndex = LayerMask.NameToLayer("Enemy");
  81. cycleTimer = 0;
  82.  
  83. if (this.GetComponent<Rigidbody>())// Check if the current object has a rigid body attatched
  84. playerRBody = this.GetComponent<Rigidbody>();
  85. else
  86. Debug.Log(this.gameObject + " needs a rigid body");
  87.  
  88. //Handle loading and grabbing character abilities/scripts here
  89. basicAttack = this.gameObject.GetComponent<BasicAttack>();
  90.  
  91. if (characterClass == CharacterClass.Paladin)
  92. {
  93. paladinEarthHealingSpring = this.GetComponent<paladinHealingSpring>();
  94. paladinTaunt = this.GetComponent<Taunt>();
  95. }
  96. else if (characterClass == CharacterClass.Assassin)
  97. {
  98. thunderStrike = this.GetComponent<ThunderStrike>();
  99. execution = this.GetComponent<Execution>();
  100. }
  101. else if (characterClass == CharacterClass.Archer)
  102. {
  103. arrowVolley = this.GetComponent<ArrowVolley>();
  104. piercingArrow = this.GetComponent<PiercingArrow>();
  105. }
  106. else if (characterClass == CharacterClass.Warrior)
  107. {
  108. warriorAxeWhirlwind = this.GetComponent<AxeWhirlwind>();
  109. warriorFalmingLeap = this.GetComponent<FlamingLeap>();
  110. }
  111. }
  112.  
  113. void FixedUpdate()
  114. {
  115. //Grab movement input x and y 2D
  116. xMove = movementInput.x;
  117. yMove = movementInput.y;
  118.  
  119. //Fill input direction with the Lerp of current pos and destination direction as well as rotation direction
  120. Vector3 targetInputDir = new Vector3(xMove, 0, yMove);
  121. inputDirection = Vector3.Lerp(inputDirection, targetInputDir, Time.deltaTime * 10f);
  122.  
  123. //Fill the desired direction, rotation vector with the basic directions
  124. Vector3 desiredDirection = new Vector3(inputDirection.x, 0, inputDirection.z);
  125.  
  126. //Verify there was input
  127. if (desiredDirection != Vector3.zero)
  128. UpdatePlayer(desiredDirection);//Move the player
  129.  
  130. //Call update rotation to change rotation of the player
  131. UpdatePlayerRotation();
  132.  
  133. //Update the cycle timer
  134. if (cycleTimer >= 0)
  135. cycleTimer -= Time.deltaTime;
  136.  
  137.  
  138.  
  139. //Draw where the player is currently facing
  140. //UsefullFunctions.DebugRay(transform.position, transform.forward * 5.0f, Color.red);
  141.  
  142. //Debug draw testing left
  143. //UsefullFunctions.DebugRay(transform.position, new Vector3( -1f, 0, 1f), Color.blue);
  144. //Debug draw testing left
  145. //UsefullFunctions.DebugRay(transform.position, new Vector3(-0.5f, 0, 1f)* 1.2f, Color.blue);
  146.  
  147. //Debug draw testing right
  148. //UsefullFunctions.DebugRay(transform.position, new Vector3( 1f, 0, 1f), Color.green);
  149. //Debug draw testing right
  150. //UsefullFunctions.DebugRay(transform.position, new Vector3( 0.5f, 0, 1f) * 1.2f, Color.green);
  151. }
  152.  
  153. /// <summary>
  154. /// Player update function handling the movement/speed of the player
  155. /// </summary>
  156. /// <param name="dir">The desired direction of the left analog stick</param>
  157. void UpdatePlayer(Vector3 dir)
  158. {
  159. //Debug.Log("Moving" + dir);
  160. //Generate the new position based on our speed and time passed
  161. dir = dir * speed * 5 * Time.deltaTime;
  162. //Update that position
  163. //playerRBody.MovePosition(transform.position + dir);
  164. //Debug.Log(dir);
  165. playerRBody.AddForce(dir * 500);
  166.  
  167.  
  168.  
  169. }
  170.  
  171. /// <summary>
  172. /// Player update rotation function handling the rotation of the player based on right analog stick input
  173. /// </summary>
  174. void UpdatePlayerRotation()
  175. {
  176. //Camera Direction for calculating rotation
  177. camForward = mainCamera.transform.forward;
  178. camRight = mainCamera.transform.right;
  179.  
  180. camForward.y = 0f;
  181. camRight.y = 0f;
  182.  
  183. Vector2 input = rotationDirection;
  184.  
  185. //Convert "input" to a Vector3 where the Y axis will be used as the Z axis
  186. Vector3 lookDirection = new Vector3(input.x, 0, input.y);
  187. //Convert the direction from local space to world space then projecting to get the Vector to rotate to
  188. Vector3 lookRot = mainCamera.transform.TransformDirection(lookDirection);
  189. lookRot = Vector3.ProjectOnPlane(lookRot, Vector3.up);
  190.  
  191. //Verify we actually have input
  192. if (lookRot != Vector3.zero)
  193. {
  194. //reset the constraints
  195. this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
  196. this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
  197. //Update the players rigidbody
  198. newRotation = Quaternion.LookRotation(lookRot);
  199. Debug.Log("updating move rotation");
  200. playerRBody.MoveRotation(newRotation);
  201. }
  202. //else we dont have input prevent spinning
  203. else
  204. {
  205. this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
  206. }
  207.  
  208. //Handle right analog targeting
  209. if (cycleTimer <= 0)
  210. {
  211. RaycastHit hit;
  212. // Does the ray intersect any objects in the enemy layer
  213. if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, 1 << enemyLayerIndex))
  214. {
  215. //Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
  216. Debug.Log("Right analog targeting did hit: " + hit.collider.gameObject.name);
  217. targetedEnemy = hit.collider.gameObject;
  218. targetedEnemy.GetComponent<AI>().isTargeted = true;
  219.  
  220. }
  221. // Else we didn't hit anyone reset the targeted enemy
  222. else
  223. {
  224. //Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
  225. //Debug.Log("Did not Hit");
  226. //if we had a targeted enemy reset the last targeted enemies field and erase our last targeted enemy field
  227. if (targetedEnemy != null)
  228. {
  229. targetedEnemy.GetComponent<AI>().isTargeted = false;
  230. targetedEnemy = null;
  231. }
  232.  
  233. }
  234. cycleTimer = cycleSpeed;//Reset timer whether we looked at a enemy or not
  235. }
  236. }
  237.  
  238. void OnEnable()
  239. {
  240. inputAction.Enable();
  241. }
  242. void OnDisable()
  243. {
  244. inputAction.Disable();
  245. }
  246.  
  247. //-------------------------------------------------------------------------------------------------------------------------------------------
  248. //Input Functions below (Examples of how the call are made context being the information given back to us by the input casting that results in our data)
  249. public void Attack(InputAction.CallbackContext context)
  250. {
  251. basicAttack.CallBasicAttack();
  252. }
  253.  
  254. public void Move(InputAction.CallbackContext context)
  255. {
  256. //Debug.Log("Move");
  257. }
  258.  
  259. public void Ability1(InputAction.CallbackContext context) // For the character's first ability (left bumper)
  260. {
  261. if (characterClass == CharacterClass.Paladin)
  262. {
  263. if (context.performed)
  264. paladinEarthHealingSpring.UseAbility();
  265. }
  266. else if (characterClass == CharacterClass.Assassin)
  267. {
  268. if (context.performed)
  269. thunderStrike.UseAbility(targetedEnemy);
  270. }
  271. else if (characterClass == CharacterClass.Archer)
  272. {
  273. if (context.performed)
  274. arrowVolley.UseAbility(targetedEnemy);
  275. }
  276. else if (characterClass == CharacterClass.Warrior)
  277. {
  278. warriorFalmingLeap.UseAbility(rotationDirection);
  279.  
  280. }
  281. }
  282.  
  283. public void Ability2(InputAction.CallbackContext context) // For the character's second ability (right bumper)
  284. {
  285. if (characterClass == CharacterClass.Paladin)
  286. {
  287. if (context.performed)
  288. paladinTaunt.tauntEnemies();
  289. }
  290. else if (characterClass == CharacterClass.Assassin)
  291. {
  292. if (context.performed)
  293. execution.UseAbility(targetedEnemy);
  294. }
  295. else if (characterClass == CharacterClass.Archer)
  296. {
  297. if (context.performed)
  298. piercingArrow.UseAbility();
  299. }
  300. else if (characterClass == CharacterClass.Warrior)
  301. {
  302. if (context.performed)
  303. warriorAxeWhirlwind.UseAbility();
  304. }
  305. }
  306.  
  307. public void PauseGame(InputAction.CallbackContext context)
  308. {
  309. if (context.performed)
  310. {
  311. FindObjectOfType<HUDController>().PauseGame();
  312. }
  313. }
  314.  
  315. public void HealthLoss()
  316. {
  317. GetComponent<Health>().DebugHealthLoss();
  318. }
  319.  
  320. public void HealthGain()
  321. {
  322. GetComponent<Health>().DebugHealthGain();
  323. }
  324.  
  325. /// <summary>
  326. /// Cycle enemy targets backwards
  327. /// </summary>
  328. /// <param name="context">Input</param>
  329. public void CycleTargetB(InputAction.CallbackContext context)
  330. {
  331. if (cycleTimer <= 0)
  332. {
  333. //cycle
  334. //Grab all colliders inside of the sphere which in our case acts as a circle with the enemy layer mask
  335. Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, cycleRange, 1 << enemyLayerIndex);
  336.  
  337. //loop through the skeletons near the player
  338. int i = 0;
  339. while (i < hitColliders.Length)
  340. {
  341. //if the enemy is targeted target the next enemy in the list
  342. if (hitColliders[i].gameObject.GetComponent<AI>().isTargeted == true)
  343. {
  344. hitColliders[i].gameObject.GetComponent<AI>().isTargeted = false;
  345. //If we have a increase in the list then increment else set the first one.
  346. if (hitColliders.Length < (i - 1))
  347. {
  348. hitColliders[i - 1].gameObject.GetComponent<AI>().isTargeted = true;
  349. targetedEnemy = hitColliders[i - 1].gameObject;
  350. Debug.Log("Current Selected Skeleton: " + hitColliders[i - 1].gameObject.name);
  351. }
  352. else
  353. {
  354. hitColliders[0].gameObject.GetComponent<AI>().isTargeted = true;
  355. targetedEnemy = hitColliders[0].gameObject;
  356. Debug.Log("Current Selected Skeleton: " + hitColliders[0].gameObject.name);
  357. }
  358. }
  359. else//If we cannot find the last targeted enemy
  360. {
  361. //Grab and reset all enemies
  362. Collider[] fixTargeting = Physics.OverlapSphere(this.transform.position, cycleRange * 10, 1 << enemyLayerIndex);
  363.  
  364. int j = 0;
  365. while (j < fixTargeting.Length)
  366. {
  367. fixTargeting[j].gameObject.GetComponent<AI>().isTargeted = false;
  368. j++;
  369. }
  370.  
  371. //Set the first enemy in the close radius of the player as the target
  372. hitColliders[0].gameObject.GetComponent<AI>().isTargeted = true;
  373. Debug.Log("Current Selected Skeleton: " + hitColliders[0].gameObject.name);
  374. }
  375. i++;
  376. }
  377.  
  378. //once done cycling reset timer
  379. cycleTimer = cycleSpeed;
  380. }
  381. Debug.Log("CycleTargetB");
  382. }
  383.  
  384. /// <summary>
  385. /// Cycle enemy targets forwards
  386. /// </summary>
  387. /// <param name="context">Input</param>
  388. public void CycleTargetF(InputAction.CallbackContext context)
  389. {
  390. if (cycleTimer <= 0)
  391. {
  392. //cycle
  393. //Grab all colliders inside of the sphere which in our case acts as a circle with the enemy layer mask
  394. Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, cycleRange, 1 << enemyLayerIndex);
  395.  
  396. //loop through the skeletons near the player
  397. int i = 0;
  398. while (i < hitColliders.Length)
  399. {
  400. //if the enemy is targeted target the next enemy in the list
  401. if (hitColliders[i].gameObject.GetComponent<AI>().isTargeted == true)
  402. {
  403. hitColliders[i].gameObject.GetComponent<AI>().isTargeted = false;
  404. //If we have a increase in the list then increment else set the first one.
  405. if (hitColliders.Length < (i + 1))
  406. {
  407. hitColliders[i + 1].gameObject.GetComponent<AI>().isTargeted = true;
  408. targetedEnemy = hitColliders[i + 1].gameObject;
  409. Debug.Log("Current Selected Skeleton: " + hitColliders[i + 1].gameObject.name + " : " + i);
  410. }
  411. else
  412. {
  413. hitColliders[0].gameObject.GetComponent<AI>().isTargeted = true;
  414. targetedEnemy = hitColliders[0].gameObject;
  415. Debug.Log("Current Selected Skeleton: " + hitColliders[0].gameObject.name);
  416. }
  417. }
  418. else//If we cannot find the last targeted enemy
  419. {
  420. //Grab and reset all enemies
  421. Collider[] fixTargeting = Physics.OverlapSphere(this.transform.position, cycleRange * 10, 1 << enemyLayerIndex);
  422.  
  423. int j = 0;
  424. while (j < fixTargeting.Length)
  425. {
  426. fixTargeting[j].gameObject.GetComponent<AI>().isTargeted = false;
  427. j++;
  428. }
  429.  
  430. //Set the first enemy in the close radius of the player as the target
  431. hitColliders[0].gameObject.GetComponent<AI>().isTargeted = true;
  432. Debug.Log("Current Selected Skeleton: " + hitColliders[0].gameObject.name);
  433. }
  434. i++;
  435. }
  436.  
  437. //once done cycling reset timer
  438. cycleTimer = cycleSpeed;
  439. }
  440. Debug.Log("CycleTargetF");
  441. }
  442.  
  443. Collider[] GetInSphereOverlap(Vector3 pos, float r, int layer)
  444. {
  445. Collider[] hitColliders = Physics.OverlapSphere(pos, r, layer);
  446.  
  447. return hitColliders;
  448. }
  449.  
  450. //public void Move(InputAction.CallbackContext context)
  451. //{
  452. //Debug.Log("Move");
  453. //}
  454. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement