Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /*
- This is code written for 2d platformer tutorial available at this link:
- https://rookj.blogspot.com/2020/04/tutorial-how-to-make-2d-platformer-in.html
- */
- public class PlatformerTutorial1 : MonoBehaviour
- {
- public PlayerPhase currentPlayerPhase;
- const string movingFloatName = "Moving";
- const string jumpBoolName = "Jump";
- const float movementSpeed = 5f, jumpPower = 7f,
- groundedGravityScale = 5f, fallingGravityScale = 5f,
- jumpGravityScale = 1f;
- public LayerMask groundedLayerMask;
- Vector2 groundOverlapArea;
- float groundOffset;
- //components
- public Rigidbody2D playerRig;
- public Animator playerAnimator;
- public SpriteRenderer playerSpriteRenderer;
- public CapsuleCollider2D playerCapsuleCollider2D;
- // Start is called before the first frame update
- void Start()
- {
- playerRig = gameObject.GetComponent<Rigidbody2D>();
- playerAnimator = gameObject.GetComponent<Animator>();
- playerSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
- playerCapsuleCollider2D = gameObject.GetComponent<CapsuleCollider2D>();
- groundOverlapArea = new Vector2((
- playerCapsuleCollider2D.size.x / 2f) * .9f, .3f);
- groundOffset = playerCapsuleCollider2D.offset.y -
- playerCapsuleCollider2D.size.y / 2f;
- }
- // Update is called once per frame
- void Update()
- {
- //float x = Input.GetAxis("Horizontal");
- float x = Input.GetAxisRaw("Horizontal");
- playerRig.velocity = new Vector2(movementSpeed * x,
- playerRig.velocity.y);
- if (currentPlayerPhase == PlayerPhase.IDLE)
- {
- if (x < 0)
- {
- playerAnimator.SetFloat(movingFloatName, 1f);
- playerSpriteRenderer.flipX = true;
- playerAnimator.speed = 1f;
- }
- else if (x > 0)
- {
- playerAnimator.SetFloat(movingFloatName, 1f);
- playerSpriteRenderer.flipX = false;
- playerAnimator.speed = 1f;
- }
- else
- {
- playerAnimator.SetFloat(movingFloatName, 0f);
- playerAnimator.speed = .5f;
- }
- if (IsGrounded())
- {
- playerRig.gravityScale = fallingGravityScale;
- if (Input.GetKeyDown(KeyCode.Space))
- {
- Jump();
- }
- }
- }
- else if (currentPlayerPhase == PlayerPhase.JUMP)
- {
- if (x < 0)
- {
- playerSpriteRenderer.flipX = true;
- }
- else if (x > 0)
- {
- playerSpriteRenderer.flipX = false;
- }
- if (IsGrounded())
- {
- if (playerRig.velocity.y <= 0)
- {
- playerAnimator.SetBool(jumpBoolName, false);
- currentPlayerPhase = PlayerPhase.IDLE;
- playerRig.gravityScale = groundedGravityScale;
- }
- }
- else if (playerRig.velocity.y <= 0)
- {
- playerRig.gravityScale = fallingGravityScale;
- }
- }
- }
- bool IsGrounded()
- {
- Vector2 a = new Vector2(
- transform.position.x - groundOverlapArea.x,
- transform.position.y + groundOffset);
- Vector2 b = new Vector2(
- transform.position.x + groundOverlapArea.x,
- transform.position.y + groundOffset -
- groundOverlapArea.y);
- return (Physics2D.OverlapArea(a, b, groundedLayerMask));
- }
- void Jump()
- {
- playerRig.gravityScale = jumpGravityScale;
- playerRig.velocity = new Vector2(playerRig.velocity.x,
- jumpPower);
- currentPlayerPhase = PlayerPhase.JUMP;
- playerAnimator.SetBool(jumpBoolName, true);
- playerAnimator.speed = 1f;
- }
- }
- public enum PlayerPhase
- {
- IDLE,JUMP
- }
Add Comment
Please, Sign In to add comment