Advertisement
OwlyOwl

platformer_move+gems

May 29th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PhysicsMovement : MonoBehaviour
  7. {
  8.     [SerializeField] private float _minGroundNormalY = .65f;
  9.     [SerializeField] private float _gravityModifier = 1f;
  10.     [SerializeField] private Vector2 _velocity;
  11.     [SerializeField] private LayerMask _layerMask;
  12.     [SerializeField] Animator animator;
  13.  
  14.     private Vector2 _targetVelocity;
  15.     private bool _grounded;
  16.     private Vector2 _groundNormal;
  17.     private Rigidbody2D _rigidbody2d;
  18.     private ContactFilter2D _contactFilter;
  19.     private RaycastHit2D[] _hitBuffer = new RaycastHit2D[16];
  20.     private List<RaycastHit2D> _hitBufferList = new List<RaycastHit2D>(16);
  21.     private bool _facingRight = true;
  22.  
  23.     private const float _minMoveDistance = 0.001f;
  24.     private const float _shellRadius = 0.01f;
  25.  
  26.     private void OnEnable()
  27.     {
  28.         _rigidbody2d = GetComponent<Rigidbody2D>();
  29.     }
  30.  
  31.     private void Start()
  32.     {
  33.         _contactFilter.useTriggers = false;
  34.         _contactFilter.SetLayerMask(_layerMask);
  35.         _contactFilter.useLayerMask = true;
  36.     }
  37.  
  38.     private void Update()
  39.     {
  40.         _targetVelocity = new Vector2(Input.GetAxis("Horizontal"), 0);
  41.  
  42.         if (Input.GetKey(KeyCode.Space) && _grounded)
  43.             _velocity.y = 5;
  44.     }
  45.  
  46.     private void FixedUpdate()
  47.     {
  48.         _velocity += _gravityModifier * Physics2D.gravity * Time.deltaTime;
  49.         _velocity.x = _targetVelocity.x;
  50.  
  51.         _grounded = false;
  52.  
  53.         Vector2 deltaPosition = _velocity * Time.deltaTime;
  54.         Vector2 moveAlongGround = new Vector2(_groundNormal.y, -_groundNormal.x);
  55.         Vector2 move = moveAlongGround * deltaPosition.x;
  56.  
  57.         Movement(move, false);
  58.  
  59.         move = Vector2.up * deltaPosition.y;
  60.  
  61.         Movement(move, true);
  62.  
  63.         animator.SetFloat("speed", 0);
  64.         if (_velocity.x != 0)
  65.         {
  66.             animator.SetFloat("speed", 1);
  67.         }
  68.  
  69.         if (_velocity.x >= 0 && !_facingRight)
  70.         {
  71.             Flip();
  72.         }
  73.         else if (_velocity.x <= 0 && _facingRight)
  74.         {
  75.             Flip();
  76.         }
  77.     }
  78.  
  79.     private void Movement(Vector2 move, bool yMovement)
  80.     {
  81.         float distance = move.magnitude;
  82.  
  83.         if (distance > _minMoveDistance)
  84.         {
  85.             int count = _rigidbody2d.Cast(move, _contactFilter, _hitBuffer, distance + _shellRadius);
  86.  
  87.  
  88.             _hitBufferList.Clear();
  89.  
  90.             for (int i = 0; i < count; i++)
  91.             {
  92.                 _hitBufferList.Add(_hitBuffer[i]);
  93.             }
  94.  
  95.             for (int i = 0; i < _hitBufferList.Count; i++)
  96.             {
  97.                 Vector2 currentNormal = _hitBufferList[i].normal;
  98.                 if (currentNormal.y > _minGroundNormalY)
  99.                 {
  100.                     _grounded = true;
  101.                     if (yMovement)
  102.                     {
  103.                         _groundNormal = currentNormal;
  104.                         currentNormal.x = 0;
  105.                     }
  106.                 }
  107.  
  108.                 float projection = Vector2.Dot(_velocity, currentNormal);
  109.                 if (projection < 0)
  110.                 {
  111.                     _velocity = _velocity - projection * currentNormal;
  112.                 }
  113.  
  114.                 float modifiedDistance = _hitBufferList[i].distance - _shellRadius;
  115.                 distance = modifiedDistance < distance ? modifiedDistance : distance;
  116.             }
  117.         }
  118.  
  119.         _rigidbody2d.position = _rigidbody2d.position + move.normalized * distance;
  120.     }
  121.  
  122.     private void Flip()
  123.     {
  124.         _facingRight = !_facingRight;
  125.         Vector3 theScale = transform.localScale;
  126.         theScale.x *= -1;
  127.         transform.localScale = theScale;
  128.     }
  129.  
  130.     private void OnTriggerEnter2D(Collider2D collision)
  131.     {
  132.         if (collision.TryGetComponent(out Gem gem))
  133.         {
  134.             Destroy(collision.gameObject);
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement