Guest User

Untitled

a guest
Feb 16th, 2019
11,785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.26 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HollowMovementScript : MonoBehaviour {
  6.  
  7.     //Hollow knight movement script for u/bigjew222
  8.     //Contains jumping, attacking, moving, and recoil mechanics.
  9.     //everything regarding the animator has been commented out so you can use it if you need to.
  10.  
  11.     //GAMEOBJECT SETUP
  12.     //A gameobject with a rigidBody2D, with rotation locked, and a Box collider.
  13.     //Five Gameobjects childed to it. One just above the collider, one just below the collider, one a distance above, one a distance below,
  14.     //and one a distance in front.
  15.     //If this is confusing just read the other comments and create them as they go.
  16.  
  17.     public PlayerStateList pState;
  18.  
  19.     [Header("X Axis Movement")]
  20.     [SerializeField] float walkSpeed = 25f;
  21.  
  22.     [Space(5)]
  23.  
  24.     [Header("Y Axis Movement")]
  25.     [SerializeField] float jumpSpeed = 45;
  26.     [SerializeField] float fallSpeed = 45;
  27.     [SerializeField] int jumpSteps = 20;
  28.     [SerializeField] int jumpThreshold = 7;
  29.     [Space(5)]
  30.  
  31.     [Header("Attacking")]
  32.     [SerializeField] float timeBetweenAttack = 0.4f;
  33.     [SerializeField] Transform attackTransform; // this should be a transform childed to the player but to the right of them, where they attack from.
  34.     [SerializeField] float attackRadius = 1;
  35.     [SerializeField] Transform downAttackTransform;//This should be a transform childed below the player, for the down attack.
  36.     [SerializeField] float downAttackRadius = 1;
  37.     [SerializeField] Transform upAttackTransform;//Same as above but for the up attack.
  38.     [SerializeField] float upAttackRadius = 1;
  39.     [SerializeField] LayerMask attackableLayer;
  40.     [Space(5)]
  41.  
  42.     [Header("Recoil")]
  43.     [SerializeField] int recoilXSteps = 4;
  44.     [SerializeField] int recoilYSteps = 10;
  45.     [SerializeField] float recoilXSpeed = 45;
  46.     [SerializeField] float recoilYSpeed = 45;
  47.     [Space(5)]
  48.  
  49.     [Header("Ground Checking")]
  50.     [SerializeField] Transform groundTransform; //This is supposed to be a transform childed to the player just under their collider.
  51.     [SerializeField] float groundCheckY = 0.2f; //How far on the Y axis the groundcheck Raycast goes.
  52.     [SerializeField] float groundCheckX = 1;//Same as above but for X.
  53.     [SerializeField] LayerMask groundLayer;
  54.     [Space(5)]
  55.  
  56.     [Header("Roof Checking")]
  57.     [SerializeField] Transform roofTransform; //This is supposed to be a transform childed to the player just above their collider.
  58.     [SerializeField] float roofCheckY = 0.2f;
  59.     [SerializeField] float roofCheckX = 1; // You probably want this to be the same as groundCheckX
  60.     [Space(5)]
  61.  
  62.  
  63.     float timeSinceAttack;
  64.     float xAxis;
  65.     float yAxis;
  66.     float grabity;
  67.     int stepsXRecoiled;
  68.     int stepsYRecoiled;
  69.     int stepsJumped = 0;
  70.  
  71.     Rigidbody2D rb;
  72.     [SerializeField] Animator anim;
  73.  
  74.     // Use this for initialization
  75.     void Start () {
  76.         if(pState == null)
  77.         {
  78.             pState = GetComponent<PlayerStateList>();
  79.         }
  80.  
  81.         rb = GetComponent<Rigidbody2D>();
  82.  
  83.         grabity = rb.gravityScale;
  84.     }
  85.    
  86.     // Update is called once per frame
  87.     void Update () {
  88.         GetInputs();
  89.  
  90.         Flip();
  91.         Walk(xAxis);
  92.         Recoil();
  93.         Attack();
  94.     }
  95.  
  96.     void FixedUpdate()
  97.     {
  98.         if (pState.recoilingX == true && stepsXRecoiled < recoilXSteps)
  99.         {
  100.             stepsXRecoiled++;
  101.         }
  102.         else
  103.         {
  104.             StopRecoilX();
  105.         }
  106.         if (pState.recoilingY == true && stepsYRecoiled < recoilYSteps)
  107.         {
  108.             stepsYRecoiled++;
  109.         }
  110.         else
  111.         {
  112.             StopRecoilY();
  113.         }
  114.         if (Grounded())
  115.         {
  116.             StopRecoilY();
  117.         }
  118.  
  119.         Jump();
  120.     }
  121.  
  122.     void Jump()
  123.     {
  124.         if (pState.jumping)
  125.         {
  126.  
  127.             if (stepsJumped < jumpSteps && !Roofed())
  128.             {
  129.                 rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
  130.                 stepsJumped++;
  131.             }
  132.             else
  133.             {
  134.                 StopJumpSlow();
  135.             }
  136.         }
  137.  
  138.         //This limits how fast the player can fall
  139.         //Since platformers generally have increased gravity, you don't want them to fall so fast they clip trough all the floors.
  140.         if (rb.velocity.y < -Mathf.Abs(fallSpeed))
  141.         {
  142.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -Mathf.Abs(fallSpeed), Mathf.Infinity));
  143.         }
  144.     }
  145.  
  146.     void Walk(float MoveDirection)
  147.     {
  148.         //Rigidbody2D rigidbody2D = rb;
  149.         //float x = MoveDirection * walkSpeed;
  150.         //Vector2 velocity = rb.velocity;
  151.         //rigidbody2D.velocity = new Vector2(x, velocity.y);
  152.         if (!pState.recoilingX)
  153.         {
  154.             rb.velocity = new Vector2(MoveDirection * walkSpeed, rb.velocity.y);
  155.  
  156.             if (Mathf.Abs(rb.velocity.x) > 0)
  157.             {
  158.                 pState.walking = true;
  159.             }
  160.             else
  161.             {
  162.                 pState.walking = false;
  163.             }
  164.             if (xAxis > 0)
  165.             {
  166.                 pState.lookingRight = true;
  167.             }
  168.             else if (xAxis < 0)
  169.             {
  170.                 pState.lookingRight = false;
  171.             }
  172.  
  173.             //anim.SetBool("Walking", pState.walking);
  174.         }
  175.  
  176.     }
  177.  
  178.     void Attack()
  179.     {
  180.         timeSinceAttack += Time.deltaTime;
  181.         if (Input.GetButtonDown("Attack") && timeSinceAttack >= timeBetweenAttack)
  182.         {
  183.             timeSinceAttack = 0;
  184.             //Attack Side
  185.             if (yAxis == 0 || yAxis < 0 && Grounded())
  186.             {
  187.                 //anim.SetTrigger("1");
  188.                 Collider2D[] objectsToHit = Physics2D.OverlapCircleAll(attackTransform.position, attackRadius, attackableLayer);
  189.                 if (objectsToHit.Length > 0)
  190.                 {
  191.                     pState.recoilingX = true;
  192.                 }
  193.                 for (int i = 0; i < objectsToHit.Length; i++)
  194.                 {
  195.                     //Here is where you would do whatever attacking does in your script.
  196.                     //In my case its passing the Hit method to an Enemy script attached to the other object(s).
  197.                 }
  198.             }
  199.             //Attack Up
  200.             else if (yAxis > 0)
  201.             {
  202.                 //anim.SetTrigger("2");
  203.                 Collider2D[] objectsToHit = Physics2D.OverlapCircleAll(upAttackTransform.position, upAttackRadius, attackableLayer);
  204.                 if (objectsToHit.Length > 0)
  205.                 {
  206.                     pState.recoilingY = true;
  207.                 }
  208.                 for (int i = 0; i < objectsToHit.Length; i++)
  209.                 {
  210.                     //Here is where you would do whatever attacking does in your script.
  211.                     //In my case its passing the Hit method to an Enemy script attached to the other object(s).
  212.                 }
  213.             }
  214.             //Attack Down
  215.             else if (yAxis < 0 && !Grounded())
  216.             {
  217.                 //anim.SetTrigger("3");
  218.                 Collider2D[] objectsToHit = Physics2D.OverlapCircleAll(downAttackTransform.position, downAttackRadius, attackableLayer);
  219.                 if (objectsToHit.Length > 0)
  220.                 {
  221.                     pState.recoilingY = true;
  222.                 }
  223.                 for (int i = 0; i < objectsToHit.Length; i++)
  224.                 {
  225.  
  226.                     //Here I commented out the actual script I use, in case you wanted to see it.
  227.  
  228.  
  229.                     /*Instantiate(slashEffect1, objectsToHit[i].transform);
  230.                     if (!(objectsToHit[i].GetComponent<EnemyV1>() == null))
  231.                     {
  232.                         objectsToHit[i].GetComponent<EnemyV1>().Hit(damage, 0, -YForce);
  233.                     }
  234.  
  235.                     if (objectsToHit[i].tag == "Enemy")
  236.                     {
  237.                         Mana += ManaGain;
  238.                     }*/
  239.                 }
  240.             }
  241.  
  242.         }
  243.     }
  244.  
  245.     void Recoil()
  246.     {
  247.         //since this is run after Walk, it takes priority, and effects momentum properly.
  248.         if (pState.recoilingX)
  249.         {
  250.             if (pState.lookingRight)
  251.             {
  252.                 rb.velocity = new Vector2(-recoilXSpeed, 0);
  253.             }
  254.             else
  255.             {
  256.                 rb.velocity = new Vector2(recoilXSpeed, 0);
  257.             }
  258.         }
  259.         if (pState.recoilingY)
  260.         {
  261.             if (yAxis < 0)
  262.             {
  263.                 rb.velocity = new Vector2(rb.velocity.x, recoilYSpeed);
  264.                 rb.gravityScale = 0;
  265.             }
  266.             else
  267.             {
  268.                 rb.velocity = new Vector2(rb.velocity.x, -recoilYSpeed);
  269.                 rb.gravityScale = 0;
  270.             }
  271.  
  272.         }
  273.         else
  274.         {
  275.             rb.gravityScale = grabity;
  276.         }
  277.     }
  278.  
  279.     void Flip()
  280.     {
  281.         if (xAxis > 0)
  282.         {
  283.             transform.localScale = new Vector2(1, transform.localScale.y);
  284.         }
  285.         else if (xAxis < 0)
  286.         {
  287.             transform.localScale = new Vector2(-1, transform.localScale.y);
  288.         }
  289.     }
  290.  
  291.     void StopJumpQuick()
  292.     {
  293.         //Stops The player jump immediately, causing them to start falling as soon as the button is released.
  294.         stepsJumped = 0;
  295.         pState.jumping = false;
  296.         rb.velocity = new Vector2(rb.velocity.x, 0);
  297.     }
  298.  
  299.     void StopJumpSlow()
  300.     {
  301.         //stops the jump but lets the player hang in the air for awhile.
  302.         stepsJumped = 0;
  303.         pState.jumping = false;
  304.     }
  305.  
  306.     void StopRecoilX()
  307.     {
  308.         stepsXRecoiled = 0;
  309.         pState.recoilingX = false;
  310.     }
  311.  
  312.     void StopRecoilY()
  313.     {
  314.         stepsYRecoiled = 0;
  315.         pState.recoilingY = false;
  316.     }
  317.  
  318.     public bool Grounded()
  319.     {
  320.         //this does three small raycasts at the specified positions to see if the player is grounded.
  321.         if (Physics2D.Raycast(groundTransform.position, Vector2.down, groundCheckY, groundLayer) || Physics2D.Raycast(groundTransform.position + new Vector3(-groundCheckX, 0), Vector2.down, groundCheckY, groundLayer) || Physics2D.Raycast(groundTransform.position + new Vector3(groundCheckX, 0), Vector2.down, groundCheckY, groundLayer))
  322.         {
  323.             return true;
  324.         }
  325.         else
  326.         {
  327.             return false;
  328.         }
  329.     }
  330.  
  331.     public bool Roofed()
  332.     {
  333.         //This does the same thing as grounded but checks if the players head is hitting the roof instead.
  334.         //Used for canceling the jump.
  335.         if (Physics2D.Raycast(roofTransform.position, Vector2.up, roofCheckY, groundLayer) || Physics2D.Raycast(roofTransform.position + new Vector3(roofCheckX, 0), Vector2.up, roofCheckY, groundLayer) || Physics2D.Raycast(roofTransform.position + new Vector3(roofCheckX, 0), Vector2.up, roofCheckY, groundLayer))
  336.         {
  337.             return true;
  338.         }
  339.         else
  340.         {
  341.             return false;
  342.         }
  343.     }
  344.  
  345.     void GetInputs()
  346.     {
  347.         //WASD/Joystick
  348.         yAxis = Input.GetAxis("Vertical");
  349.         xAxis = Input.GetAxis("Horizontal");
  350.  
  351.         //This is essentially just sensitivity.
  352.         if (yAxis > 0.25)
  353.         {
  354.             yAxis = 1;
  355.         }
  356.         else if (yAxis < -0.25)
  357.         {
  358.             yAxis = -1;
  359.         }
  360.         else
  361.         {
  362.             yAxis = 0;
  363.         }
  364.  
  365.         if (xAxis > 0.25)
  366.         {
  367.             xAxis = 1;
  368.         }
  369.         else if (xAxis < -0.25)
  370.         {
  371.             xAxis = -1;
  372.         }
  373.         else
  374.         {
  375.             xAxis = 0;
  376.         }
  377.  
  378.         //anim.SetBool("Grounded", Grounded());
  379.         //anim.SetFloat("YVelocity", rb.velocity.y);
  380.  
  381.         //Jumping
  382.         if (Input.GetButtonDown("Jump") && Grounded())
  383.         {
  384.             pState.jumping = true;
  385.         }
  386.  
  387.         if (!Input.GetButton("Jump") && stepsJumped < jumpSteps && stepsJumped > jumpThreshold && pState.jumping)
  388.         {
  389.             StopJumpQuick();
  390.         }
  391.         else if (!Input.GetButton("Jump") && stepsJumped < jumpThreshold && pState.jumping)
  392.         {
  393.             StopJumpSlow();
  394.         }
  395.     }
  396.  
  397.     void OnDrawGizmos()
  398.     {
  399.         Gizmos.color = Color.red;
  400.         Gizmos.DrawWireSphere(attackTransform.position, attackRadius);
  401.         Gizmos.DrawWireSphere(downAttackTransform.position, downAttackRadius);
  402.         Gizmos.DrawWireSphere(upAttackTransform.position, upAttackRadius);
  403.         //Gizmos.DrawWireCube(groundTransform.position, new Vector2(groundCheckX, groundCheckY));
  404.  
  405.         Gizmos.DrawLine(groundTransform.position, groundTransform.position + new Vector3(0, -groundCheckY));
  406.         Gizmos.DrawLine(groundTransform.position + new Vector3(-groundCheckX, 0), groundTransform.position + new Vector3(-groundCheckX, -groundCheckY));
  407.         Gizmos.DrawLine(groundTransform.position + new Vector3(groundCheckX, 0), groundTransform.position + new Vector3(groundCheckX, -groundCheckY));
  408.  
  409.         Gizmos.DrawLine(roofTransform.position, roofTransform.position + new Vector3(0, roofCheckY));
  410.         Gizmos.DrawLine(roofTransform.position + new Vector3(-roofCheckX, 0), roofTransform.position + new Vector3(-roofCheckX, roofCheckY));
  411.         Gizmos.DrawLine(roofTransform.position + new Vector3(roofCheckX, 0), roofTransform.position + new Vector3(roofCheckX, roofCheckY));
  412.     }
  413. }
Add Comment
Please, Sign In to add comment