Search Unity

2D collision problem with Y-axis(No rigidbody nor character controller)

Discussion in 'Physics' started by OVERprogramming, Jan 10, 2020.

  1. OVERprogramming

    OVERprogramming

    Joined:
    Jan 10, 2020
    Posts:
    1
    Hi, I was trying to make a rougelite game like moonlighter with some of my friends.
    We wanted to try making them without rigidbody and character controller.(IDK what my friends are thinking.)

    So here is the problem:


    Error.gif



    Even with "Auto Sync Transforms" on in Physic2D project setting, it jitters if it collides while moving downwards.
    And it passes through thin colliders but only with Y-axis, it even doesn't go through edge collider when moving horizontally.
    It's a lot easier to go through while moving downwards.


    errorsc2.gif



    I wanna get the jittering fixed and be able to collide properly even with a thin collider such as edge collider or a small box collider.(Even while sprinting, which increases speed by 1.6x!)


    errorsc3.gif



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private float speed = 6.4f;
    8.     private float acceleration = 64;
    9.     private float deceleration = 96;
    10.     private float sprint = 1.6f;
    11.     private float roll = 2.3f;
    12.     private float time = 9.98f;
    13.     public float angle = 0.0f;
    14.  
    15.  
    16.     private Vector2 velocity;
    17.     private Vector2 direction;
    18.     public Vector3 battleBox;
    19.  
    20.  
    21.     private BoxCollider2D playerCollider;
    22.     public CapsuleDirection2D facing;
    23.  
    24.  
    25.     //public Animator playerAnimator;
    26.  
    27.  
    28.     private void Awake()
    29.     {
    30.         playerCollider = GetComponent<BoxCollider2D>();
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         DirectionInput();
    36.  
    37.         MoveInput();
    38.  
    39.         Collide();
    40.     }
    41.  
    42.     void FixedUpdate()
    43.     {
    44.         MoveOutput();
    45.     }
    46.  
    47.  
    48.     void DirectionInput ()
    49.     {
    50.         direction.x = Input.GetAxisRaw("Horizontal");
    51.         direction.y = Input.GetAxisRaw("Vertical");
    52.  
    53.         direction.Normalize();
    54.     }
    55.  
    56.     void MoveInput ()
    57.     {
    58.         if (direction != Vector2.zero)
    59.         {
    60.             if (Input.GetButton("Sprint"))
    61.             {
    62.                 velocity.x = Mathf.MoveTowards(velocity.x, speed * sprint * direction.x, acceleration * sprint * Time.deltaTime);
    63.                 velocity.y = Mathf.MoveTowards(velocity.y, speed * sprint * direction.y, acceleration * sprint * Time.deltaTime);
    64.             }
    65.             else
    66.             {
    67.                 velocity.x = Mathf.MoveTowards(velocity.x, speed * direction.x, acceleration * Time.deltaTime);
    68.                 velocity.y = Mathf.MoveTowards(velocity.y, speed * direction.y, acceleration * Time.deltaTime);
    69.             }
    70.         }
    71.         else
    72.         {
    73.             velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
    74.             velocity.y = Mathf.MoveTowards(velocity.y, 0, deceleration * Time.deltaTime);
    75.         }
    76.     }
    77.  
    78.     void MoveOutput ()
    79.     {
    80.         transform.Translate(velocity * Time.fixedDeltaTime);
    81.     }
    82.  
    83.     public void Facing ()
    84.     {
    85.         DirectionInput();
    86.  
    87.         if (Mathf.Abs(direction.x) + Mathf.Abs(direction.y) == 1)
    88.         {
    89.             angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90;
    90.  
    91.             transform.eulerAngles = new Vector3(0, 0, angle);
    92.  
    93.             if (angle % 180 == 0 || direction == Vector2.down)
    94.             {
    95.                 facing = CapsuleDirection2D.Horizontal;
    96.             }
    97.             else
    98.             {
    99.                 facing = CapsuleDirection2D.Vertical;
    100.             }
    101.         }
    102.  
    103.         if (direction != Vector2.zero)
    104.         {
    105.             battleBox = direction / 2;
    106.         }
    107.     }
    108.  
    109.     void Collide ()
    110.     {
    111.         Collider2D[] playerCollisions = Physics2D.OverlapBoxAll(playerCollider.transform.position, playerCollider.size, 0);
    112.  
    113.         foreach (Collider2D collision in playerCollisions)
    114.         {
    115.             if (collision == collision.CompareTag("Player"))
    116.             {
    117.                 continue;
    118.             }
    119.  
    120.             ColliderDistance2D colliderDistance = collision.Distance(playerCollider);
    121.  
    122.             if (colliderDistance.isOverlapped)
    123.             {
    124.                 transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
    125.             }
    126.         }
    127.     }
    128.  
    Help me please ;D