Advertisement
Guest User

test

a guest
Aug 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8.     public float speed;
  9.     public float jumpForce;
  10.     private float moveInput;
  11.  
  12.     private Rigidbody2D rb;
  13.  
  14.     private bool facingRight = true;
  15.  
  16.     private bool isGrounded;
  17.     public Transform groundCheck;
  18.     public float checkRadius;
  19.     public LayerMask whatIsGround;
  20.  
  21.     private float extraJumps;
  22.     public int extraJumpsValue;
  23.  
  24.  
  25.  
  26.     void Start()
  27.     {
  28.         extraJumps = extraJumpsValue;
  29.         rb = GetComponent<Rigidbody2D>();
  30.     }
  31.  
  32.     void FixedUpdate()
  33.     {
  34.  
  35.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
  36.            
  37.  
  38.  
  39.         moveInput = Input.GetAxis("Horizontal");
  40.  
  41.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  42.  
  43.         if(facingRight == false && moveInput > 0)
  44.         {
  45.  
  46.             Flip();
  47.         } else if(facingRight == true && moveInput < 0)
  48.         {
  49.  
  50.             Flip();
  51.         }
  52.  
  53.     }
  54.  
  55.  
  56.     void Update()
  57.     {
  58.  
  59.         if(isGrounded == true)
  60.         {
  61.  
  62.             extraJumps = extraJumpsValue;
  63.         }
  64.  
  65.         if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
  66.         {
  67.  
  68.             rb.velocity = Vector2.up * jumpForce;
  69.             extraJumps--;
  70.         } else if(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
  71.         {
  72.  
  73.             rb.velocity = Vector2.up * jumpForce;
  74.         }
  75.     }
  76.  
  77.     void Flip()
  78.     {
  79.  
  80.         facingRight = !facingRight;
  81.         Vector3 Scaler = transform.localScale;
  82.         Scaler.x *= -1;
  83.         transform.localScale = Scaler;
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement