Search Unity

Physics 2d

Discussion in 'Getting Started' started by SanderJan, Apr 12, 2019.

  1. SanderJan

    SanderJan

    Joined:
    Apr 12, 2019
    Posts:
    2
    I want to create a 2d platformer and used the PhysicsObject from "https://unity3d.com/learn/tutorials/topics/2d-game-creation/player-controller-script". But when using this script the Player slides on the ground after you release the key and my question is how to remove that sliding thing from the script?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Physics : MonoBehaviour
    6. {
    7.  
    8.     public float minGroundNormalY = .65f;
    9.     public float gravityModifier = 1f;
    10.  
    11.     protected Vector2 targetVelocity;
    12.     protected bool grounded;
    13.     protected Vector2 groundNormal;
    14.     protected Rigidbody2D rb2d;
    15.     protected Vector2 velocity;
    16.     protected ContactFilter2D contactFilter;
    17.     protected RaycastHit2D[] hitBuffer = new RaycastHit2D[16];
    18.     protected List<RaycastHit2D> hitBufferList = new List<RaycastHit2D>(16);
    19.  
    20.  
    21.     protected const float minMoveDistance = 0.001f;
    22.     protected const float shellRadius = 0.01f;
    23.  
    24.     void OnEnable()
    25.     {
    26.         rb2d = GetComponent<Rigidbody2D>();
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.         contactFilter.useTriggers = false;
    32.         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
    33.         contactFilter.useLayerMask = true;
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.         targetVelocity = Vector2.zero;
    39.         ComputeVelocity();
    40.     }
    41.  
    42.     protected virtual void ComputeVelocity()
    43.     {
    44.  
    45.     }
    46.  
    47.     void FixedUpdate()
    48.     {
    49.         velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
    50.         velocity.x = targetVelocity.x;
    51.  
    52.         Vector2 deltaPosition = velocity * Time.deltaTime;
    53.  
    54.         Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);
    55.  
    56.         Vector2 move = moveAlongGround * deltaPosition.x;
    57.  
    58.         Movement(move, false);
    59.  
    60.         move = Vector2.up * deltaPosition.y;
    61.  
    62.         Movement(move, true);
    63.     }
    64.  
    65.     void Movement(Vector2 move, bool yMovement)
    66.     {
    67.         float distance = move.magnitude;
    68.  
    69.         if (distance > minMoveDistance)
    70.         {
    71.             int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
    72.             hitBufferList.Clear();
    73.             for (int i = 0; i < count; i++)
    74.             {
    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. }