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 Player : MonoBehaviour
- {
- //Movement Speed default value
- public float StartMovementSpeed = 1.5f;
- //Movement speed multiplier
- public float MoveSpeedMultiplier = 3.2f;
- //Jump power value
- public float JumpPower = 10f;
- //the grounded bool, which determines if the player can jump or not
- private bool Grounded;
- //References
- private Rigidbody2D playerRigidbody;
- private Collider2D playerCollider;
- [SerializeField]
- private LayerMask GroundLayer;
- private Animator playerAnimator;
- private void Start()
- {
- //creates the references for some of the player components
- playerRigidbody = gameObject.GetComponent<Rigidbody2D>();
- playerCollider = gameObject.GetComponent<Collider2D>();
- playerAnimator = gameObject.GetComponent<Animator>();
- }
- private void Update()
- {
- //calls the movement function every frame
- Movement();
- }
- private void FixedUpdate()
- {
- //increases the move speed every few frames
- StartMovementSpeed = StartMovementSpeed + (MoveSpeedMultiplier * Time.deltaTime);
- }
- private void Movement()
- {
- //moves the player right, with increasing speed
- playerRigidbody.velocity = new Vector2(StartMovementSpeed, playerRigidbody.velocity.y);
- //sets the grounded bool depending if the player's collider is touching the groundLayer++
- Grounded = Physics2D.IsTouchingLayers(playerCollider, GroundLayer);
- if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
- {
- if(Grounded == true)
- {
- //adds the jump force to the player's y axis
- playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, playerRigidbody.velocity.y + JumpPower);
- }
- }
- //sets the animator component's grounded value
- playerAnimator.SetBool("Grounded", Grounded);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement