Advertisement
LeeMace

Unity's 2D Physics Script for 2D Scrollers Modified by Thomas Brush

Feb 9th, 2023 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. //The following script was sourced from Unity's Platformer tutorial here: https://learn.unity.com/tutorial/live-session-2d-platformer-character-controller
  6.  
  7. // The modification is on line 81. it is something that allows the player to jump through platforms instead of hitting their heads on it.
  8.  
  9. public class PhysicsObject : MonoBehaviour
  10. {
  11.  
  12.     [Header("Physics Object Attributes")]
  13.     public float minGroundNormalY = .65f;
  14.     public float gravityModifier = 1f;
  15.  
  16.     protected Vector2 targetVelocity;
  17.     protected bool grounded;
  18.     protected Vector2 groundNormal;
  19.     protected Rigidbody2D rb2d;
  20.     protected Vector2 velocity;
  21.     protected ContactFilter2D contactFilter;
  22.     protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
  23.     protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
  24.  
  25.  
  26.     protected const float minMoveDistance = 0.001f;
  27.     protected const float shellRadius = 0.01f;
  28.  
  29.     void OnEnable()
  30.     {
  31.         rb2d = GetComponent<Rigidbody2D>();
  32.     }
  33.  
  34.     void Start()
  35.     {
  36.         contactFilter.useTriggers = false;
  37.         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
  38.         contactFilter.useLayerMask = true;
  39.     }
  40.  
  41.     void Update()
  42.     {
  43.         targetVelocity = Vector2.zero;
  44.         ComputeVelocity();
  45.     }
  46.  
  47.     protected virtual void ComputeVelocity()
  48.     {
  49.  
  50.     }
  51.  
  52.     void FixedUpdate()
  53.     {
  54.         velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
  55.         velocity.x = targetVelocity.x;
  56.  
  57.         grounded = false;
  58.  
  59.         Vector2 deltaPosition = velocity * Time.deltaTime;
  60.  
  61.         Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);
  62.  
  63.         Vector2 move = moveAlongGround * deltaPosition.x;
  64.  
  65.         Movement(move, false);
  66.  
  67.         move = Vector2.up * deltaPosition.y;
  68.  
  69.         Movement(move, true);
  70.     }
  71.  
  72.     void Movement(Vector2 move, bool yMovement)
  73.     {
  74.         float distance = move.magnitude;
  75.  
  76.         if (distance > minMoveDistance)
  77.         {
  78.             int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
  79.             hitBufferList.Clear();
  80.  
  81.             for (int i = 0; i < count; i++)
  82.             {
  83.                 PlatformEffector2D platform = hitBuffer[i].collider.GetComponent<PlatformEffector2D>();
  84.                 if (!platform || (hitBuffer[i].normal == Vector2.up && velocity.y < 0 && yMovement))
  85.                 {
  86.                     hitBufferList.Add(hitBuffer[i]);
  87.                 }
  88.             }
  89.  
  90.             for (int i = 0; i < hitBufferList.Count; i++)
  91.             {
  92.                 Vector2 currentNormal = hitBufferList[i].normal;
  93.                 if (currentNormal.y > minGroundNormalY)
  94.                 {
  95.                     grounded = true;
  96.                     if (yMovement)
  97.                     {
  98.                         groundNormal = currentNormal;
  99.                         currentNormal.x = 0;
  100.                     }
  101.                 }
  102.  
  103.                 float projection = Vector2.Dot(velocity, currentNormal);
  104.                 if (projection < 0)
  105.                 {
  106.                     velocity = velocity - projection * currentNormal;
  107.                 }
  108.  
  109.                 float modifiedDistance = hitBufferList[i].distance - shellRadius;
  110.                 distance = modifiedDistance < distance ? modifiedDistance : distance;
  111.             }
  112.  
  113.         }
  114.  
  115.         rb2d.position = rb2d.position + move.normalized * distance;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement