Advertisement
LeeMace

Unity's 2D Physics Script for 2D Scrollers

Feb 9th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PhysicsObject : MonoBehaviour {
  6.  
  7.     public float minGroundNormalY = .65f;
  8.     public float gravityModifier = 1f;
  9.  
  10.     protected Vector2 targetVelocity;
  11.     protected bool grounded;
  12.     protected Vector2 groundNormal;
  13.     protected Rigidbody2D rb2d;
  14.     protected Vector2 velocity;
  15.     protected ContactFilter2D contactFilter;
  16.     protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
  17.     protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D> (16);
  18.  
  19.  
  20.     protected const float minMoveDistance = 0.001f;
  21.     protected const float shellRadius = 0.01f;
  22.  
  23.     void OnEnable()
  24.     {
  25.         rb2d = GetComponent<Rigidbody2D> ();
  26.     }
  27.  
  28.     void Start ()
  29.     {
  30.         contactFilter.useTriggers = false;
  31.         contactFilter.SetLayerMask (Physics2D.GetLayerCollisionMask (gameObject.layer));
  32.         contactFilter.useLayerMask = true;
  33.     }
  34.  
  35.     void Update ()
  36.     {
  37.         targetVelocity = Vector2.zero;
  38.         ComputeVelocity ();    
  39.     }
  40.  
  41.     protected virtual void ComputeVelocity()
  42.     {
  43.  
  44.     }
  45.  
  46.     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.  
  55.         Vector2 moveAlongGround = new Vector2 (groundNormal.y, -groundNormal.x);
  56.  
  57.         Vector2 move = moveAlongGround * deltaPosition.x;
  58.  
  59.         Movement (move, false);
  60.  
  61.         move = Vector2.up * deltaPosition.y;
  62.  
  63.         Movement (move, true);
  64.     }
  65.  
  66.     void Movement(Vector2 move, bool yMovement)
  67.     {
  68.         float distance = move.magnitude;
  69.  
  70.         if (distance > minMoveDistance)
  71.         {
  72.             int count = rb2d.Cast (move, contactFilter, hitBuffer, distance + shellRadius);
  73.             hitBufferList.Clear ();
  74.             for (int i = 0; i < count; i++) {
  75.                 hitBufferList.Add (hitBuffer [i]);
  76.             }
  77.  
  78.             for (int i = 0; i < hitBufferList.Count; i++)
  79.             {
  80.                 Vector2 currentNormal = hitBufferList [i].normal;
  81.                 if (currentNormal.y > minGroundNormalY)
  82.                 {
  83.                     grounded = true;
  84.                     if (yMovement)
  85.                     {
  86.                         groundNormal = currentNormal;
  87.                         currentNormal.x = 0;
  88.                     }
  89.                 }
  90.  
  91.                 float projection = Vector2.Dot (velocity, currentNormal);
  92.                 if (projection < 0)
  93.                 {
  94.                     velocity = velocity - projection * currentNormal;
  95.                 }
  96.  
  97.                 float modifiedDistance = hitBufferList [i].distance - shellRadius;
  98.                 distance = modifiedDistance < distance ? modifiedDistance : distance;
  99.             }
  100.  
  101.  
  102.         }
  103.  
  104.         rb2d.position = rb2d.position + move.normalized * distance;
  105.     }
  106.  
  107. }
Tags: physics Unity 2d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement