首页 > 解决方案 > 在 box colliders 和 tilemaps 之间遇到一个奇怪的碰撞错误

问题描述

这是我的玩家移动代码:

错误和检查器元素的镜头:

https://imgur.com/a/bmGqL1M

正如你在这个 Imgur 视频中看到的那样,出于某种原因,我的玩家角色以及一个可推动的板条箱可以剪辑到这个瓷砖地图中几个像素。我不太清楚为什么会发生这种情况,因为我已经在 tilemap 中添加了一个复合 collider2D,以摆脱非常常见的“卡在 tilemap”错误,这里不是这种情况,剪裁没有t 实际上以任何方式改变运动。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [Header("Movement")]
    [SerializeField] private float movementSpeed;
    [SerializeField] private float jumpForce;

    [Header("Jumping")]
    private bool isGrounded;
    [SerializeField] private Transform feetPos;
    [SerializeField] private float checkRadius;
    [SerializeField] private LayerMask whatIsGround;
    [SerializeField] private float hangTime;
    private float hangCounter;

    private Rigidbody2D rb;
    private SpriteRenderer sr;

    private float moveX;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        GetInput();
        BetterJump();
    }

    void BetterJump()
    {
        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

        if (isGrounded)
        {
            hangCounter = hangTime;
        } else
        {
            hangCounter -= Time.deltaTime;
        }

            if (hangCounter > 0 && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = Vector2.up * jumpForce;
        }


        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * .5f);
        }
    }

    void FixedUpdate()
    {
        Move();
    }

    void Move()
    {
        rb.position += new Vector2(moveX, 0) * Time.deltaTime * movementSpeed;
    }

    void GetInput()
    {
        moveX = Input.GetAxisRaw("Horizontal");

        if(moveX < 0)
        {
            sr.flipX = true;
        }
        else if (moveX > 0)
        {
            sr.flipX = false;
        }
    }
}

标签: c#unity3dcollider

解决方案


推荐阅读