Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerBehaviour : MonoBehaviour
  6. {
  7.     // Variable set for calculation in update
  8.  
  9.     // Max speed, in units per second, that the character moves.
  10.     float speed = 9;
  11.     // Acceleration while grounded
  12.     float walkAcceleration = 75;
  13.     // Acceleration while in the air
  14.     float airAcceleration = 30;
  15.     // Deceleration applied when character is grounded and not attempting to move
  16.     float groundDeceleration = 70;
  17.     // Max height the character will jump regardless of gravity
  18.     float jumpHeight = 4;
  19.  
  20.  
  21.     // Main Unity Physics ref
  22.     private Vector2 velocity;
  23.  
  24.     // Additional ref
  25.     private Rigidbody2D rb;
  26.     private BoxCollider2D boxCollider;
  27.  
  28.     // State for checking
  29.     public enum State
  30.     {
  31.         Ground = 0,
  32.         Jump = 1,
  33.         Fall = 2,
  34.     }
  35.  
  36.     private State state = State.Ground;
  37.  
  38.     // Update is called once per frame
  39.     void Update()
  40.     {
  41.         float moveInput = Input.GetAxisRaw("Horizontal");
  42.         velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, walkAcceleration * Time.deltaTime);
  43.  
  44.         transform.Translate(velocity * Time.deltaTime);
  45.  
  46.         if (moveInput != 0)
  47.         {
  48.             velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, walkAcceleration * Time.deltaTime);
  49.         }
  50.         else
  51.         {
  52.             velocity.x = Mathf.MoveTowards(velocity.x, 0, groundDeceleration * Time.deltaTime);
  53.         }
  54.  
  55.         if (state == State.Ground && Input.GetButtonDown("Jump"))
  56.         {
  57.             velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
  58.  
  59.             velocity.y += Physics2D.gravity.y * Time.deltaTime;
  60.         }
  61.     }
  62.  
  63.     void FixedUpdate()
  64.     {
  65.         //CameraWork goes here if nessary
  66.     }
  67.  
  68.     void OnCollisionEnter2D(Collision2D collision)
  69.     {
  70.         if (collision.gameObject.tag == "Platform")
  71.         {
  72.             if (collision.gameObject.tag == "Platform")
  73.             {
  74.                 state = State.Ground;
  75.             }
  76.         }
  77.     }
  78.     void OnCollisionExit2D(Collision2D collision)
  79.     {
  80.         if (collision.gameObject.tag == "Platform")
  81.         {
  82.             if (collision.gameObject.tag == "Platform")
  83.             {
  84.                 if (state == State.Ground)
  85.                 {
  86.                     state = State.Fall;
  87.                 }
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement