Advertisement
Guest User

Player movement script unity

a guest
Apr 28th, 2020
2,891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(BoxCollider2D))]
  6. public class PlrMovement : MonoBehaviour
  7. {
  8.     [SerializeField, Tooltip("Max speed, in units per second, that the character moves.")]
  9.     float speed = 9;
  10.  
  11.     [SerializeField, Tooltip("Acceleration while grounded.")]
  12.     float walkAcceleration = 75;
  13.  
  14.     [SerializeField, Tooltip("Acceleration while in the air.")]
  15.     float airAcceleration = 30;
  16.  
  17.     [SerializeField, Tooltip("Deceleration applied when character is grounded and not attempting to move.")]
  18.     float groundDeceleration = 70;
  19.  
  20.     [SerializeField, Tooltip("Max height the character will jump regardless of gravity")]
  21.     float jumpHeight = 4;
  22.  
  23.     private BoxCollider2D boxCollider;
  24.  
  25.     private Vector2 velocity;
  26.  
  27.    
  28.     private bool grounded;
  29.  
  30.     private void Awake()
  31.     {
  32.         boxCollider = GetComponent<BoxCollider2D>();
  33.     }
  34.  
  35.     private void Update()
  36.     {
  37.        
  38.         float moveInput = Input.GetAxisRaw("Horizontal");
  39.  
  40.         if (grounded)
  41.         {
  42.             velocity.y = 0;
  43.  
  44.             if (Input.GetButtonDown("Jump"))
  45.             {
  46.                
  47.                 velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
  48.             }
  49.         }
  50.  
  51.         float acceleration = grounded ? walkAcceleration : airAcceleration;
  52.         float deceleration = grounded ? groundDeceleration : 0;
  53.  
  54.         if (moveInput != 0)
  55.         {
  56.             velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
  57.         }
  58.         else
  59.         {
  60.             velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
  61.         }
  62.  
  63.         velocity.y += Physics2D.gravity.y * Time.deltaTime;
  64.  
  65.         transform.Translate(velocity * Time.deltaTime);
  66.  
  67.         grounded = false;
  68.  
  69.        
  70.         Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);
  71.  
  72.         foreach (Collider2D hit in hits)
  73.         {
  74.          
  75.             if (hit == boxCollider)
  76.                 continue;
  77.  
  78.             ColliderDistance2D colliderDistance = hit.Distance(boxCollider);
  79.  
  80.            
  81.             if (colliderDistance.isOverlapped)
  82.             {
  83.                 transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
  84.  
  85.                
  86.                 if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
  87.                 {
  88.                     grounded = true;
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement