Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Movement : MonoBehaviour
- {
- [Header("Component References")]
- public CharacterController2D controller;
- public Animator animator;
- public Transform WallHitBoxCheck; //For wall sliding circle cast to detect if the player is next to a wall.
- public RaycastHit2D BackWallHit; //For checking if the player is near a wall while facing the other direction.
- public Collider2D FrontCircleCast;
- public RaycastHit2D GroundDistanceCheck;
- public RaycastHit2D GroundFalloffCast;
- public Transform GroundFalloffTransform;
- public ParticleSystem SpellParticle01;
- public ParticleSystem SpellParticle02;
- public GameObject Nebula;
- Rigidbody2D m_rigidbody2D; //Needs some WD-40.
- [HideInInspector] public float ScaleAtJump;
- [HideInInspector] public float LockXMultiplier = 1;
- public float FalloffCastDistance;
- public float GroundCastDistance;
- public float CoyoteWallJumpTime;
- public float WallCastDistance = 1;
- public float WallHitBoxDistance;
- public float MoveSpeed;
- public float HardFallVelocity = -30.0f;
- public float MaxWallslideVelocity;
- public float AttackPushForce;
- [HideInInspector] public float JumpDirection;
- [HideInInspector] public float MoveSpeedReset; //Resets the player's movement speed after it is altered by certain animations.
- [Range(0, 1)] public float HardLandingSlowdown;
- [Range(0, 1)] public float AttackSpeedSlowdown;
- [HideInInspector] public bool IsAttacking = false;
- [HideInInspector] public bool AttackListening = false;
- [HideInInspector] public int AttackInputs = 0;
- public bool LockXMovement = false;
- public bool CurrentlyWallSliding = false;
- float HorizontalMove;
- float ForceMultiplier;
- [HideInInspector] public bool CoyoteWallJump = false;
- [SerializeField] bool jump = false;
- [SerializeField] bool crouch = false;
- bool runonce = true;
- private void Start()
- {
- m_rigidbody2D = GetComponent<Rigidbody2D>();
- MoveSpeedReset = MoveSpeed;
- }
- void Update()
- { //((!ng && !ws) || (ng && ws))
- HorizontalMove = (Input.GetAxisRaw("Horizontal") * MoveSpeed);
- animator.SetBool("isCrouching", crouch); animator.SetBool("Airborn", !controller.m_Grounded);
- GroundDistanceCheck = Physics2D.Raycast(transform.position, -transform.up, GroundCastDistance, LayerMask.GetMask("Ground"));
- Debug.DrawRay(transform.position, -transform.up * GroundCastDistance, Color.cyan);
- FrontCircleCast = Physics2D.OverlapCircle(WallHitBoxCheck.position, WallHitBoxDistance, LayerMask.GetMask("Ground"));
- 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)))
- { 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.
- BackWallHit = Physics2D.Raycast(transform.position, -transform.right * transform.localScale.x, WallCastDistance, LayerMask.GetMask("Ground"));
- GroundFalloffCast = Physics2D.Raycast(GroundFalloffTransform.position, -GroundFalloffTransform.up, FalloffCastDistance, LayerMask.GetMask("Ground"));
- Debug.DrawRay(GroundFalloffTransform.position, -GroundFalloffTransform.up * FalloffCastDistance, Color.magenta);
- if (AttackListening && Input.GetButtonDown("Fire1")) { AttackInputs++; }
- if (Input.GetAxisRaw("Horizontal") != 0) { animator.SetBool("isMoving", true); } else { animator.SetBool("isMoving", false); }
- if (Input.GetButtonDown("Crouch")) { crouch = true; } else if (Input.GetButtonUp("Crouch")) { crouch = false; }
- if (Input.GetButtonDown("Jump") && !crouch)
- {
- ScaleAtJump = transform.localScale.x; jump = true;
- if (controller.m_Grounded) { animator.SetTrigger("Jumped"); }
- }
- if (crouch && Input.GetButtonDown("Jump")) { animator.Play("Player_SlideStart"); }
- if (Input.GetButtonDown("Fire1") && !IsAttacking && !animator.GetBool("isWallSliding") && controller.m_Grounded) { Attack(); }
- if (Input.GetButtonDown("Fire2") && !IsAttacking && !animator.GetBool("isWallSliding") && controller.m_Grounded) { CastSpell(); }
- if (Input.GetButtonDown("Fire3") && !IsAttacking && !animator.GetBool("isWallSliding") && controller.m_Grounded) { TakeDamage(); } // TEST
- }
- void FixedUpdate()
- {
- controller.Move(HorizontalMove * Time.fixedDeltaTime, crouch, jump);
- jump = false;
- animator.SetFloat("y_Velocity", m_rigidbody2D.velocity.y);
- if (LockXMovement && !GroundFalloffCast)//this one bitch
- {
- m_rigidbody2D.velocity = new Vector2(Mathf.Clamp(m_rigidbody2D.velocity.x * LockXMultiplier, -100.0f, 0f), m_rigidbody2D.velocity.y);
- //m_rigidbody2D.AddForce(new Vector2(-m_rigidbody2D.velocity.x * ForceMultiplier, 0f), ForceMode2D.Impulse);
- }
- if (!controller.m_Grounded && m_rigidbody2D.velocity.y < HardFallVelocity) { animator.SetBool("longFall", true); }
- if (controller.wasGrounded) { animator.SetBool("longFall", false); }
- if (CurrentlyWallSliding)
- {
- m_rigidbody2D.velocity = new Vector2(m_rigidbody2D.velocity.x, Mathf.Clamp(m_rigidbody2D.velocity.y, MaxWallslideVelocity, 0)); //Clamp the y velocity when wallsliding
- }
- }
- public void Attack()
- {
- animator.Play("Player_SwordAttack"); AttackListening = true; IsAttacking = true; MoveSpeed *= AttackSpeedSlowdown;
- }
- public void CastSpell()
- {
- animator.Play("Player_SpellCast"); IsAttacking = true; MoveSpeed *= AttackSpeedSlowdown;
- }
- public void ContinueAttack(int lap)
- {
- if (lap == 1 && AttackInputs < 1) { animator.Play("Player_IdleSword"); }
- if (lap == 2 && AttackInputs < 2) { animator.Play("Player_IdleSword"); }
- }
- public IEnumerator CoyoteWallJumpRoutine() { CoyoteWallJump = true; yield return new WaitForSeconds(CoyoteWallJumpTime); CoyoteWallJump = false; } //Only allows coyote jump for small period of time.
- public void HardLanding() { MoveSpeed *= HardLandingSlowdown; } //HardLanding and HardLandingEnd are called by a state machine when the player falls a great distance.
- public void HardLandingEnd() { if (!IsAttacking) { MoveSpeed = MoveSpeedReset; } }
- public void AttackPush(float multiplier)
- {
- ForceMultiplier = multiplier;
- m_rigidbody2D.AddForce(new Vector2(AttackPushForce * multiplier * transform.localScale.x, 0f), ForceMode2D.Impulse);
- //NOTE: Possibly try adding a velocity instead of an impulse.
- }
- public void InstantiateSpell()
- {
- GameObject spell = Instantiate(Nebula, new Vector2(transform.position.x + (2.0f * transform.localScale.x), transform.position.y + 0.25f), Quaternion.identity) as GameObject;
- NebulaScript nebulascript = spell.GetComponent<NebulaScript>();
- if (transform.localScale.x < 1) { nebulascript.Direction = -1; }
- }
- public void TakeDamage()
- {
- animator.Play("Player_Hurt"); MoveSpeed *= AttackSpeedSlowdown;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement