Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerBehavior : MonoBehaviour
  6. {
  7.  
  8.     // Player Movement Variable
  9.     public float MoveSpeed;
  10.     public float JumpHeight;
  11.     private bool doubleJump;
  12.  
  13.     // Player Grounded Variables
  14.     public Transform groundCheck;
  15.     public float groundCheckRadius;
  16.     public LayerMask whatIsGround;
  17.     public bool grounded;
  18.  
  19.     //Non-Stick Player
  20.     private float moveVelocity;
  21.  
  22.     // public Animator animator;
  23.  
  24.     // Use this for initialization
  25.     void Start()
  26.     {
  27.         // Animation Reset
  28.         // animator.SetBool("IsWalking", false);
  29.         // animator.SetBool("IsJumping", false);
  30.     }
  31.  
  32.  
  33.     void FixedUpdate()
  34.     {
  35.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
  36.     }
  37.  
  38.  
  39.     // Update is called once per frame
  40.     void Update()
  41.     {
  42.         // This code makes the character jump
  43.         if (Input.GetKeyDown(KeyCode.Space) && grounded)
  44.         {
  45.             Jump();
  46.         }
  47.  
  48.         // Double Jump code
  49.         if (grounded)
  50.         {
  51.             doubleJump = false;
  52.             // animator.SetBool("IsJumping", false);
  53.         }
  54.  
  55.         if (Input.GetKeyDown(KeyCode.Space) && !doubleJump && !grounded)
  56.         {
  57.             Jump();
  58.             doubleJump = true;
  59.         }
  60.  
  61.         // This code makes the character move from side to side using the A & D keys
  62.         if (Input.GetKey(KeyCode.D))
  63.         {
  64.             //  GetComponent<Rigidbody2D>().velocity = new Vector2(MoveSpeed, GetComponent<Rigidbody2D>().velocity.y);
  65.             moveVelocity = MoveSpeed;
  66.             // animator.SetBool("IsWalking", true);
  67.         }
  68.  
  69.         else if (Input.GetKeyUp(KeyCode.D))
  70.         {
  71.             // animator.SetBool("IsWalking", false);
  72.         }
  73.  
  74.  
  75.         if (Input.GetKey(KeyCode.A))
  76.         {
  77.             //  GetComponent<Rigidbody2D>().velocity = new Vector2(-MoveSpeed, GetComponent<Rigidbody2D>().velocity.y);
  78.             moveVelocity = -MoveSpeed;
  79.             // animator.SetBool("IsWalking", true);
  80.         }
  81.  
  82.         else if (Input.GetKeyUp(KeyCode.A))
  83.         {
  84.             // animator.SetBool("IsWalking", false);
  85.         }
  86.  
  87.         GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
  88.  
  89.         // Player flip
  90.         if (GetComponent<Rigidbody2D>().velocity.x > 0)
  91.             transform.localScale = new Vector3(0.2f, 0.25f, 0f);
  92.  
  93.         else if (GetComponent<Rigidbody2D>().velocity.x < 0)
  94.             transform.localScale = new Vector3(-0.2f, 0.25f, 0f);
  95.  
  96.         //Non-Stick Player
  97.         moveVelocity = 0f;
  98.     }
  99.  
  100.     public void Jump()
  101.     {
  102.         GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
  103.         // animator.SetBool("IsJumping", true);
  104.     }
  105.  
  106.     void OnCollisionEnter2D(Collision2D col)
  107.     {
  108.       if (col.collider.tag == "MovingPlatform")
  109.            this.transform.parent = col.transform;
  110.     }
  111.     void OnCollisionExit2D(Collision2D col)
  112.     {
  113.       if (col.collider.tag == "MovingPlatform")
  114.            this.transform.parent = null;
  115.     }
  116.  
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement