Advertisement
Guest User

Untitled

a guest
Sep 13th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Movement : MonoBehaviour
  6. {
  7. [Header("Component References")]
  8. public CharacterController2D controller;
  9. public Animator animator;
  10. public Transform WallHitBoxCheck; //For wall sliding circle cast to detect if the player is next to a wall.
  11. public RaycastHit2D BackWallHit; //For checking if the player is near a wall while facing the other direction.
  12. public Collider2D FrontCircleCast;
  13. public RaycastHit2D GroundDistanceCheck;
  14. public RaycastHit2D GroundFalloffCast;
  15. public Transform GroundFalloffTransform;
  16. public ParticleSystem SpellParticle01;
  17. public ParticleSystem SpellParticle02;
  18. public GameObject Nebula;
  19.  
  20. Rigidbody2D m_rigidbody2D; //Needs some WD-40.
  21.  
  22. [HideInInspector] public float ScaleAtJump;
  23. [HideInInspector] public float LockXMultiplier = 1;
  24. public float FalloffCastDistance;
  25. public float GroundCastDistance;
  26. public float CoyoteWallJumpTime;
  27. public float WallCastDistance = 1;
  28. public float WallHitBoxDistance;
  29. public float MoveSpeed;
  30. public float HardFallVelocity = -30.0f;
  31. public float MaxWallslideVelocity;
  32. public float AttackPushForce;
  33. [HideInInspector] public float JumpDirection;
  34. [HideInInspector] public float MoveSpeedReset; //Resets the player's movement speed after it is altered by certain animations.
  35. [Range(0, 1)] public float HardLandingSlowdown;
  36. [Range(0, 1)] public float AttackSpeedSlowdown;
  37. [HideInInspector] public bool IsAttacking = false;
  38. [HideInInspector] public bool AttackListening = false;
  39. [HideInInspector] public int AttackInputs = 0;
  40. public bool LockXMovement = false;
  41. public bool CurrentlyWallSliding = false;
  42.  
  43. float HorizontalMove;
  44. float ForceMultiplier;
  45. [HideInInspector] public bool CoyoteWallJump = false;
  46. [SerializeField] bool jump = false;
  47. [SerializeField] bool crouch = false;
  48.  
  49. bool runonce = true;
  50. private void Start()
  51. {
  52. m_rigidbody2D = GetComponent<Rigidbody2D>();
  53. MoveSpeedReset = MoveSpeed;
  54. }
  55.  
  56. void Update()
  57. { //((!ng && !ws) || (ng && ws))
  58. HorizontalMove = (Input.GetAxisRaw("Horizontal") * MoveSpeed);
  59. animator.SetBool("isCrouching", crouch); animator.SetBool("Airborn", !controller.m_Grounded);
  60.  
  61. GroundDistanceCheck = Physics2D.Raycast(transform.position, -transform.up, GroundCastDistance, LayerMask.GetMask("Ground"));
  62. Debug.DrawRay(transform.position, -transform.up * GroundCastDistance, Color.cyan);
  63. FrontCircleCast = Physics2D.OverlapCircle(WallHitBoxCheck.position, WallHitBoxDistance, LayerMask.GetMask("Ground"));
  64. if ((FrontCircleCast && !controller.m_Grounded && m_rigidbody2D.velocity.y < -0.1) && ((Input.GetAxisRaw("Horizontal") > 0 && controller.m_FacingRight) || (Input.GetAxisRaw("Horizontal") < 0 && !controller.m_FacingRight)))
  65. { if (((!GroundDistanceCheck && !CurrentlyWallSliding) || (GroundDistanceCheck && CurrentlyWallSliding))) { animator.SetBool("isWallSliding", true); } } else { animator.SetBool("isWallSliding", false); } // Circle touching wall while not grounded and moving downward, AND moving in direction of wall.
  66.  
  67. BackWallHit = Physics2D.Raycast(transform.position, -transform.right * transform.localScale.x, WallCastDistance, LayerMask.GetMask("Ground"));
  68. GroundFalloffCast = Physics2D.Raycast(GroundFalloffTransform.position, -GroundFalloffTransform.up, FalloffCastDistance, LayerMask.GetMask("Ground"));
  69. Debug.DrawRay(GroundFalloffTransform.position, -GroundFalloffTransform.up * FalloffCastDistance, Color.magenta);
  70.  
  71. if (AttackListening && Input.GetButtonDown("Fire1")) { AttackInputs++; }
  72. if (Input.GetAxisRaw("Horizontal") != 0) { animator.SetBool("isMoving", true); } else { animator.SetBool("isMoving", false); }
  73. if (Input.GetButtonDown("Crouch")) { crouch = true; } else if (Input.GetButtonUp("Crouch")) { crouch = false; }
  74. if (Input.GetButtonDown("Jump") && !crouch)
  75. {
  76. ScaleAtJump = transform.localScale.x; jump = true;
  77. if (controller.m_Grounded) { animator.SetTrigger("Jumped"); }
  78. }
  79. if (crouch && Input.GetButtonDown("Jump")) { animator.Play("Player_SlideStart"); }
  80. if (Input.GetButtonDown("Fire1") && !IsAttacking && !animator.GetBool("isWallSliding") && controller.m_Grounded) { Attack(); }
  81. if (Input.GetButtonDown("Fire2") && !IsAttacking && !animator.GetBool("isWallSliding") && controller.m_Grounded) { CastSpell(); }
  82. if (Input.GetButtonDown("Fire3") && !IsAttacking && !animator.GetBool("isWallSliding") && controller.m_Grounded) { TakeDamage(); } // TEST
  83. }
  84.  
  85. void FixedUpdate()
  86. {
  87. controller.Move(HorizontalMove * Time.fixedDeltaTime, crouch, jump);
  88. jump = false;
  89. animator.SetFloat("y_Velocity", m_rigidbody2D.velocity.y);
  90.  
  91. if (LockXMovement && !GroundFalloffCast)//this one bitch
  92. {
  93. m_rigidbody2D.velocity = new Vector2(Mathf.Clamp(m_rigidbody2D.velocity.x * LockXMultiplier, -100.0f, 0f), m_rigidbody2D.velocity.y);
  94. //m_rigidbody2D.AddForce(new Vector2(-m_rigidbody2D.velocity.x * ForceMultiplier, 0f), ForceMode2D.Impulse);
  95. }
  96.  
  97. if (!controller.m_Grounded && m_rigidbody2D.velocity.y < HardFallVelocity) { animator.SetBool("longFall", true); }
  98. if (controller.wasGrounded) { animator.SetBool("longFall", false); }
  99. if (CurrentlyWallSliding)
  100. {
  101. m_rigidbody2D.velocity = new Vector2(m_rigidbody2D.velocity.x, Mathf.Clamp(m_rigidbody2D.velocity.y, MaxWallslideVelocity, 0)); //Clamp the y velocity when wallsliding
  102. }
  103.  
  104. }
  105.  
  106. public void Attack()
  107. {
  108. animator.Play("Player_SwordAttack"); AttackListening = true; IsAttacking = true; MoveSpeed *= AttackSpeedSlowdown;
  109. }
  110. public void CastSpell()
  111. {
  112. animator.Play("Player_SpellCast"); IsAttacking = true; MoveSpeed *= AttackSpeedSlowdown;
  113. }
  114.  
  115. public void ContinueAttack(int lap)
  116. {
  117. if (lap == 1 && AttackInputs < 1) { animator.Play("Player_IdleSword"); }
  118. if (lap == 2 && AttackInputs < 2) { animator.Play("Player_IdleSword"); }
  119. }
  120. public IEnumerator CoyoteWallJumpRoutine() { CoyoteWallJump = true; yield return new WaitForSeconds(CoyoteWallJumpTime); CoyoteWallJump = false; } //Only allows coyote jump for small period of time.
  121.  
  122. public void HardLanding() { MoveSpeed *= HardLandingSlowdown; } //HardLanding and HardLandingEnd are called by a state machine when the player falls a great distance.
  123. public void HardLandingEnd() { if (!IsAttacking) { MoveSpeed = MoveSpeedReset; } }
  124.  
  125. public void AttackPush(float multiplier)
  126. {
  127. ForceMultiplier = multiplier;
  128. m_rigidbody2D.AddForce(new Vector2(AttackPushForce * multiplier * transform.localScale.x, 0f), ForceMode2D.Impulse);
  129. //NOTE: Possibly try adding a velocity instead of an impulse.
  130. }
  131.  
  132. public void InstantiateSpell()
  133. {
  134. GameObject spell = Instantiate(Nebula, new Vector2(transform.position.x + (2.0f * transform.localScale.x), transform.position.y + 0.25f), Quaternion.identity) as GameObject;
  135. NebulaScript nebulascript = spell.GetComponent<NebulaScript>();
  136. if (transform.localScale.x < 1) { nebulascript.Direction = -1; }
  137. }
  138.  
  139. public void TakeDamage()
  140. {
  141. animator.Play("Player_Hurt"); MoveSpeed *= AttackSpeedSlowdown;
  142. }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement