Home>

I am making a 2D action game by performing my own physics in C #.
As shown in the following image, dents occur at the corners of the block.
There is no image, but when you jump from the bottom and hit the lower corner of the block, a sinking occurs, and the character will be lifted upwards so that it escapes from the blocking with the block Has also happened.


If i look at the collider in the scene tab, the collider looks like it's not colliding, so it's more strange.

The code that moves the player is the following two, and the block uses Tilemap to make the Collider a Grid. In addition, it is CompositeCollider. The player uses BoxCollider.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KinematicObject: MonoBehaviour
{
    Rigidbody2D rb2d;
    public float minGroundNormalY = 0.65f;
    public float gravityModifier = 1f;
    protected bool isGrounded;
    protected Vector2 groundNormal;
    protected Vector2 targetVelocity;
    protected Vector2 velocity;
    protected ContactFilter2D contactFilter;
    protected RaycastHit2D [] hitBuffer = new RaycastHit2D [16];
    protected List<RaycastHit2D>hitBufferList = new List<RaycastHit2D>(16);
    protected const float minMoveDistance = 0.001f;
    protected const float shellRadius = 0.01f;
    protected const float minVelocityY = -10f;
    void OnEnable ()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }
    void Start ()
    {
        contactFilter.useTriggers = false;
        contactFilter.SetLayerMask (Physics2D.GetLayerCollisionMask (gameObject.layer));
        contactFilter.useLayerMask = true;
        groundNormal = Vector2.up;
    }
    void Update ()
    {
        targetVelocity = Vector2.zero;
        ComputeVelocity ();
    }
    protected virtual void ComputeVelocity ()
    {
    }
    void FixedUpdate ()
    {if (! isGrounded&&velocity.y<0)
        {
            velocity + = 1.25f * gravityModifier * Physics2D.gravity * Time.deltaTime;
        }
        else
        {
            velocity + = gravityModifier * Physics2D.gravity * Time.deltaTime;
        }
        velocity.x = targetVelocity.x;
        velocity.y = velocity.y>minVelocityY? velocity.y: minVelocityY;
        isGrounded = false;
        Vector2 deltaPosition = velocity * Time.deltaTime;
        Vector2 moveAlongGround = new Vector2 (groundNormal.y, -groundNormal.x);
        Vector2 move = deltaPosition.x * moveAlongGround;
        PerformMovement (move, false);
        move = Vector2.up * deltaPosition.y;
        PerformMovement (move, true);
    }
    void PerformMovement (Vector2 move, bool yMovement)
    {
        float distance = move.magnitude;
        if (distance>minMoveDistance)
        {
            int count = rb2d.Cast (move, contactFilter, hitBuffer, distance + shellRadius);
            hitBufferList.Clear ();
            for (int i = 0;i<count;i ++)
            {
                hitBufferList.Add (hitBuffer [i]);
            }
            for (int i = 0;i<hitBufferList.Count;i ++)
            {
                Vector2 currentNormal = hitBufferList [i] .normal;
                if (currentNormal.y>minGroundNormalY)
                {
                    isGrounded = true;
                    if (yMovement)
                    {
                        groundNormal = currentNormal;
                        currentNormal.x = 0;
                    }
                }
                float projection = Vector2.Dot (velocity, currentNormal);
                if (projection<0)
                {
                    velocity = velocity-projection * currentNormal;
                }float modifiedDistance = hitBufferList [i] .distance-shellRadius;
                distance = modifiedDistance<distance? modifiedDistance: distance;
            }
        }
        rb2d.position = rb2d.position + distance * move.normalized;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController: KinematicObject
{
    public float maxSpeed ​​= 5f;
    public float jumpTakeOffSpeed ​​= 8f;
    private SpriteRenderer spriteRenderer;
    private Animator animator;
    void Awake ()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator = GetComponent  ();
    }
    protected override void ComputeVelocity ()
    {
        Vector2 move = Vector2.zero;
        move.x = Input.GetAxis ("Horizontal");
        if (Input.GetButtonDown ("Jump")&&isGrounded)
        {
            velocity.y = jumpTakeOffSpeed;
        }
        else if (Input.GetButtonUp ("Jump"))
        {
            if (velocity.y>0) velocity.y = velocity.y * 0.5f;
        }
        bool flipSprite = (spriteRenderer.flipX? (move.x>0.01f): (move.x<-0.01f));
        if (flipSprite) {
            spriteRenderer.flipX =! spriteRenderer.flipX;
        }
        targetVelocity = move * maxSpeed;
        animator.SetBool ("isGrounded", isGrounded);
        animator.SetFloat ("velocityX", Mathf.Abs (velocity.x)/maxSpeed);
        animator.SetFloat ("velocityY", velocity.y);
    }
}


I'm sorry if the code is difficult to understand. I am in trouble without knowing the cause of the above phenomenon in the code.
If there is a person who understands the cause, please give me an answer.