c# - 检查是否接地而不使用 oncollision 进入/退出?
问题描述
所以我有一个脚本可以旋转一个立方体(按下方向时旋转 90 度),目前我正在使用 oncollision enter 来检测墙壁,当我尝试使用它来检查是否接地时,它会在我离开时完全停止一切一次碰撞并进入另一次碰撞,否则当我在半空中时它不会完全检查,并且当我不想移动时允许我移动。非常感谢任何和所有帮助。
{
bool isGrounded = true;
private Rigidbody2D rb;
bool leftInput = true;
bool rightInput = true;
public float RollingDuration = 0.2f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// fixed update is for physics
void FixedUpdate()
{
var dir = Vector3.zero;
if (Input.GetKey(KeyCode.LeftArrow) && leftInput && isGrounded)
{
dir = Vector3.left;
}
if (Input.GetKey(KeyCode.RightArrow) && rightInput && isGrounded)
{
dir = Vector3.right;
}
if (dir !=Vector3.zero && !isRolling)
{
StartCoroutine(Roll(dir));
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//check to see if our player is grounded
if (collision.gameObject.tag == "Grounded")
{
isGrounded = true;
}
if (collision.gameObject.tag == "LeftWall" || collision.gameObject.tag == "RightWall")
{
StopCoroutine("Roll");
if (collision.gameObject.tag == "LeftWall")
{
leftInput = false;
// we are grounded when touching walls
isGrounded = true;
}
if (collision.gameObject.tag == "RightWall")
{
rightInput = false;
// we are grounded when touching walls
isGrounded = true;
}
if (collision.gameObject.tag == null)
{
isGrounded = false;
}
else
{
isRolling = false;
rb.velocity = Vector2.zero;
rb.velocity = new Vector2(0.0f, 0.0f);
rb.angularVelocity = 0.0f;
rb.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
rb.rotation = 0.0f;
}
}
}
bool isRolling = false;
IEnumerator Roll(Vector3 direction)
{
if (direction == Vector3.right && isGrounded)
{
leftInput = true;
}
else if (direction == Vector3.left && isGrounded)
{
rightInput = true;
}
isRolling = true;
var rotAxis = Vector3.Cross(Vector3.up, direction);
var pivot = (rb.transform.position + Vector3.down * 0.5f) + direction * 0.5f;
var startRotation = rb.transform.rotation;
var endRotation = Quaternion.AngleAxis(90.0f, rotAxis) * startRotation;
var startPosition = rb.transform.position;
var endPosition = rb.transform.position + direction;
var rotSpeed = 90.0f / RollingDuration;
var t = 0.0f;
while (t < RollingDuration && isGrounded)
{
t += Time.deltaTime;
if (t < RollingDuration && isGrounded)
{
rb.transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime);
yield return null;
}
else
{
rb.transform.rotation = endRotation;
rb.transform.position = endPosition;
}
}
isRolling = false;
}
}
解决方案
我认为进行地面检查的最简单和最常见的方法是投射光线。因此,只需将光线投射到您要检查的方向或尝试重叠功能。
例子:
if(Physics.OverlapCircle(point, radius, layerMask)) {
//do stuff
}
因此,只需向您要检查的所有方向投射光线,或在立方体的每一侧添加重叠功能。
推荐阅读
- android - 如何修复 Windows 10 上的 Android Studio adb 错误?
- wifi - 如何通过按钮触发将深度睡眠结合到 ESP8266-01
- mongodb - addfields 聚合字段实际上是否将字段添加到集合中?
- java - 从 PubSub 到 Java API 的 Snowplow 数据处理
- sql - SQL 中的 ALL 函数背后的逻辑。我需要一些关于 W3 School 关于 ALL 功能的声明的逻辑背后的解释
- python - 如何使用openpyxl通过python更改数据连接源
- java - 为什么不显示数据库中的用户?
- dart - 我试图在 dart 中 switch 语句的 case 子句中定义一个范围,但无法正确设置
- javascript - 为什么 Instagram Embed 没有显示在我的 react 应用程序上?
- html - 如何设置比例取决于屏幕比例?