首页 > 解决方案 > 检查是否接地而不使用 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;

}

}

标签: c#unity3drotationgame-physicsrigid-bodies

解决方案


我认为进行地面检查的最简单和最常见的方法是投射光线。因此,只需将光线投射到您要检查的方向或尝试重叠功能。

例子:

if(Physics.OverlapCircle(point, radius, layerMask)) {
    //do stuff
}

因此,只需向您要检查的所有方向投射光线,或在立方体的每一侧添加重叠功能。


推荐阅读