rj1twitch

PlatformerTutorial1.cs

Apr 5th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. /*
  7. This is code written for 2d platformer tutorial available at this link:
  8. https://rookj.blogspot.com/2020/04/tutorial-how-to-make-2d-platformer-in.html
  9. */
  10.  
  11. public class PlatformerTutorial1 : MonoBehaviour
  12. {
  13.  
  14.  
  15.     public PlayerPhase currentPlayerPhase;
  16.  
  17.  
  18.    
  19.  
  20.  
  21.  
  22.     const string movingFloatName = "Moving";
  23.     const string jumpBoolName = "Jump";
  24.  
  25.     const float movementSpeed = 5f, jumpPower = 7f,
  26.         groundedGravityScale = 5f, fallingGravityScale = 5f,
  27.         jumpGravityScale = 1f;
  28.  
  29.     public LayerMask groundedLayerMask;
  30.  
  31.     Vector2 groundOverlapArea;
  32.     float groundOffset;
  33.  
  34.     //components
  35.     public Rigidbody2D playerRig;
  36.     public Animator playerAnimator;
  37.     public SpriteRenderer playerSpriteRenderer;
  38.     public CapsuleCollider2D playerCapsuleCollider2D;
  39.  
  40.  
  41.    
  42.     // Start is called before the first frame update
  43.     void Start()
  44.     {
  45.         playerRig = gameObject.GetComponent<Rigidbody2D>();
  46.         playerAnimator = gameObject.GetComponent<Animator>();
  47.         playerSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
  48.         playerCapsuleCollider2D = gameObject.GetComponent<CapsuleCollider2D>();
  49.  
  50.  
  51.  
  52.         groundOverlapArea = new Vector2((
  53.             playerCapsuleCollider2D.size.x / 2f) * .9f, .3f);
  54.         groundOffset = playerCapsuleCollider2D.offset.y -
  55.             playerCapsuleCollider2D.size.y / 2f;
  56.     }
  57.  
  58.     // Update is called once per frame
  59.     void Update()
  60.     {
  61.  
  62.         //float x = Input.GetAxis("Horizontal");
  63.         float x = Input.GetAxisRaw("Horizontal");
  64.  
  65.         playerRig.velocity = new Vector2(movementSpeed * x,
  66.             playerRig.velocity.y);
  67.  
  68.  
  69.  
  70.         if (currentPlayerPhase == PlayerPhase.IDLE)
  71.         {
  72.             if (x < 0)
  73.             {
  74.                 playerAnimator.SetFloat(movingFloatName, 1f);
  75.                 playerSpriteRenderer.flipX = true;
  76.                 playerAnimator.speed = 1f;
  77.             }
  78.             else if (x > 0)
  79.             {
  80.                 playerAnimator.SetFloat(movingFloatName, 1f);
  81.                 playerSpriteRenderer.flipX = false;
  82.                 playerAnimator.speed = 1f;
  83.             }
  84.             else
  85.             {
  86.                
  87.                 playerAnimator.SetFloat(movingFloatName, 0f);
  88.                 playerAnimator.speed = .5f;
  89.             }
  90.  
  91.  
  92.  
  93.             if (IsGrounded())
  94.             {
  95.                 playerRig.gravityScale = fallingGravityScale;
  96.  
  97.                
  98.  
  99.  
  100.                 if (Input.GetKeyDown(KeyCode.Space))
  101.                 {
  102.                     Jump();
  103.                 }
  104.  
  105.  
  106.  
  107.  
  108.             }
  109.            
  110.  
  111.  
  112.         }
  113.         else if (currentPlayerPhase == PlayerPhase.JUMP)
  114.         {
  115.             if (x < 0)
  116.             {
  117.                
  118.                
  119.                 playerSpriteRenderer.flipX = true;
  120.                
  121.             }
  122.             else if (x > 0)
  123.             {
  124.                
  125.                
  126.                 playerSpriteRenderer.flipX = false;
  127.                
  128.             }
  129.            
  130.  
  131.  
  132.             if (IsGrounded())
  133.             {
  134.                 if (playerRig.velocity.y <= 0)
  135.                 {
  136.                     playerAnimator.SetBool(jumpBoolName, false);
  137.                     currentPlayerPhase = PlayerPhase.IDLE;
  138.                     playerRig.gravityScale = groundedGravityScale;
  139.                 }
  140.                
  141.  
  142.  
  143.  
  144.  
  145.             }
  146.             else if (playerRig.velocity.y <= 0)
  147.             {
  148.  
  149.                 playerRig.gravityScale = fallingGravityScale;
  150.             }
  151.            
  152.  
  153.  
  154.  
  155.         }
  156.  
  157.  
  158.        
  159.     }
  160.  
  161.     bool IsGrounded()
  162.     {
  163.         Vector2 a = new Vector2(
  164.             transform.position.x - groundOverlapArea.x,
  165.             transform.position.y + groundOffset);
  166.         Vector2 b = new Vector2(
  167.             transform.position.x + groundOverlapArea.x,
  168.             transform.position.y + groundOffset -
  169.             groundOverlapArea.y);
  170.  
  171.  
  172.  
  173.         return (Physics2D.OverlapArea(a, b, groundedLayerMask));
  174.     }
  175.  
  176.  
  177.  
  178.  
  179.     void Jump()
  180.     {
  181.  
  182.  
  183.  
  184.  
  185.         playerRig.gravityScale = jumpGravityScale;
  186.  
  187.         playerRig.velocity = new Vector2(playerRig.velocity.x,
  188.             jumpPower);
  189.  
  190.  
  191.         currentPlayerPhase = PlayerPhase.JUMP;
  192.  
  193.         playerAnimator.SetBool(jumpBoolName, true);
  194.         playerAnimator.speed = 1f;
  195.     }
  196.  
  197.    
  198. }
  199.  
  200. public enum PlayerPhase
  201. {
  202.     IDLE,JUMP
  203. }
Add Comment
Please, Sign In to add comment