c# - 在 box colliders 和 tilemaps 之间遇到一个奇怪的碰撞错误
问题描述
这是我的玩家移动代码:
错误和检查器元素的镜头:
正如你在这个 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# - 使用不同 Ifc 版本的相同代码的不同结果
- r - 如何根据 R 中的第一原理绘制双变量密度?
- python - 调用函数直到输入为“否”
- node.js - 无法安装包,因为它没有 package.json
- javascript - Javascript element.click() 不适用于 div 标签
- sharepoint - Power BI - 使用 SharePoint 的术语库/托管元数据层次结构
- react-hooks - 在反应中显示确认消息 2 次
- php - 使用 php 中的 API 访问 Google My Business Statistics
- reactjs - 将状态共享给其他组件 | 将数据传递给其他组件
- gitlab-ci-runner - Gitlab Runner 在启动下一个管道之前等待