Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.  
  8.     //Movement Speed default value
  9.     public float StartMovementSpeed = 1.5f;
  10.     //Movement speed multiplier
  11.     public float MoveSpeedMultiplier = 3.2f;
  12.     //Jump power value
  13.     public float JumpPower = 10f;
  14.  
  15.     //the grounded bool, which determines if the player can jump or not
  16.     private bool Grounded;
  17.  
  18.     //References
  19.     private Rigidbody2D playerRigidbody;
  20.     private Collider2D playerCollider;
  21.     [SerializeField]
  22.     private LayerMask GroundLayer;
  23.     private Animator playerAnimator;
  24.    
  25.  
  26.  
  27.     private void Start()
  28.     {
  29.         //creates the references for some of the player components
  30.         playerRigidbody = gameObject.GetComponent<Rigidbody2D>();
  31.         playerCollider = gameObject.GetComponent<Collider2D>();
  32.         playerAnimator = gameObject.GetComponent<Animator>();
  33.     }
  34.  
  35.     private void Update()
  36.     {
  37.         //calls the movement function every frame
  38.         Movement();
  39.     }
  40.  
  41.     private void FixedUpdate()
  42.     {
  43.         //increases the move speed every few frames
  44.         StartMovementSpeed = StartMovementSpeed + (MoveSpeedMultiplier * Time.deltaTime);
  45.     }
  46.  
  47.  
  48.     private void Movement()
  49.     {
  50.         //moves the player right, with increasing speed
  51.         playerRigidbody.velocity = new Vector2(StartMovementSpeed, playerRigidbody.velocity.y);
  52.  
  53.         //sets the grounded bool depending if the player's collider is touching the groundLayer++
  54.         Grounded = Physics2D.IsTouchingLayers(playerCollider, GroundLayer);
  55.  
  56.         if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
  57.         {
  58.             if(Grounded == true)
  59.             {
  60.                 //adds the jump force to the player's y axis
  61.                 playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, playerRigidbody.velocity.y + JumpPower);
  62.             }
  63.         }
  64.  
  65.         //sets the animator component's grounded value
  66.         playerAnimator.SetBool("Grounded", Grounded);
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement