Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6.  
  7.     public float moveSpeed;
  8.     private float moveVelocity;
  9.     public float jumpHeight;
  10.  
  11.     public Transform groundCheck;
  12.     public float groundCheckRadius;
  13.     public LayerMask whatIsGround;
  14.     private bool grounded;
  15.  
  16.     private bool doubleJumped;
  17.  
  18.     private Animator anim;
  19.  
  20.     void Start()
  21.     {
  22.         anim = GetComponent<Animator>();
  23.     }
  24.  
  25.     void FixedUpdate()
  26.     {
  27.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
  28.     }
  29.  
  30.    
  31.  
  32.     void Update () {
  33.  
  34.         if (grounded)
  35.         {
  36.             doubleJumped = false;
  37.         }
  38.  
  39.         anim.SetBool("Grounded", grounded);
  40.  
  41.         if (Input.GetKeyDown(KeyCode.Space) && grounded)
  42.         {
  43.             Jump();
  44.         }
  45.  
  46.         if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
  47.         {
  48.             Jump();
  49.             doubleJumped = true;
  50.         }
  51.  
  52.         moveVelocity = 0f;
  53.  
  54.         if (Input.GetKey(KeyCode.D))
  55.         {
  56.             moveVelocity = moveSpeed;
  57.         }
  58.  
  59.         if (Input.GetKey(KeyCode.A))
  60.         {
  61.             moveVelocity = -moveSpeed;
  62.         }
  63.  
  64.         if (Input.GetKey(KeyCode.S))
  65.         {
  66.             anim.SetBool("Holding S", true);
  67.         }
  68.  
  69.         if (Input.GetKeyUp(KeyCode.S))
  70.         {
  71.             anim.SetBool("Holding S", false);
  72.         }
  73.  
  74.         GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
  75.  
  76.         anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
  77.  
  78.     }
  79.  
  80.     public void Jump()
  81.     {
  82.         GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement