【问题标题】:Character Stuck On Walls人物卡在墙上
【发布时间】:2016-10-01 04:24:11
【问题描述】:

我最近开始使用 Unity2D,并将一个简单的角色控制器与刚体和碰撞器组合在一起。现在它的设置方式是一个用于脚的圆形碰撞器和一个用于身体其余部分的箱形碰撞器。我这样做的原因是脚上的盒子碰撞器会导致角色有时会卡住在瓷砖上移动。我试图解决的问题发生在角色被压在墙上时。在水平移动进入墙壁时,重力和跳跃似乎没有任何影响。他被困在墙上。我相信这与摩擦有关,因为当我将块材料设置为 0 摩擦时,他不再有这个问题。但是,当我这样做时,由于他脚上的圆形对撞机,他变得足够滑以从块的边缘滑落。任何建议/修复将不胜感激。

public class SpriteController : MonoBehaviour {

    public float maxSpeed = 2f;
    bool facingRight = true;

    Animator anim;

    bool grounded = false;
    bool swimming = false;
    public Transform groundCheck;
    float groundRadius = 0.05f;
    public LayerMask whatIsGround;
    public float jumpForce = 700f;
    public float swimForce = 10f;
    public PhysicsMaterial2D myMaterial;

    void Start () 
    {
        anim = GetComponent<Animator> ();
    }

    void FixedUpdate () 
    {
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool ("Ground", grounded);

        anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);


        float move = Input.GetAxis ("Horizontal");

        anim.SetFloat ("Speed", Mathf.Abs (move));

        rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);

        if (move > 0 && !facingRight)
                Flip ();
        else if (move < 0 && facingRight)
                Flip ();
    }

    void Update()
    {
        Spin();
        if (grounded && Input.GetKeyDown (KeyCode.Space)) 
        {
            anim.SetBool ("Ground", false);
            rigidbody2D.AddForce(new Vector2(0, jumpForce));
        }
        else if(swimming && Input.GetKey (KeyCode.Space)) rigidbody2D.AddForce(new Vector2(0, swimForce));
    }
    void Spin()
    {
        if(Input.GetKeyDown ("f")) anim.SetBool("Spin", true);
        if(Input.GetKeyUp ("f")) anim.SetBool("Spin", false);
    }
    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Water") {
            rigidbody2D.drag = 15;
            swimming = true;
            grounded = false;
        }
    }
    void OnTriggerExit2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Water"){
            rigidbody2D.drag = 0;
            swimming = false;
        }
    }
}

【问题讨论】:

  • 来自角色控制器统一文档。我记得它注意到角色皮肤深度也会卡住。检查文档。
  • 精灵使用 2d 精灵表,所以我看不出皮肤深度会有多大问题。但是我可能会误解您的评论。

标签: unity3d unityscript


【解决方案1】:

摩擦力设置为 0 应该没问题。角色从方块边缘开始滑落多远?您可以尝试缩小圆形对撞机相对于角色的大小(尤其是宽度)。如果您的所有其他物理工作正常,则可能与圆形对撞机的定位或大小有关。

【讨论】:

  • 这是评论,不是答案。请使用问题下方的 cmets 部分来解决此类问题
  • 这似乎更像是一个临时解决方案而不是永久解决方案,因为我将来可能需要改变摩擦。例如,许多瓷砖地图编辑器不允许使用physics2d 材质,因此摩擦调整很困难。
【解决方案2】:

在这里,我的修复对我来说效果很好。 我刚刚检查了玩家是否与非地面的物体发生碰撞。在这种特殊情况下,我禁用了播放器(用户输入)移动:

void FixedUpdate () 
{
    grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    anim.SetBool ("Ground", grounded);

    anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);

    // -- JUST ADD THIS --
    if (!grounded && rigidbody2D.IsTouchingLayers ()) {
        return;
    }
    // -- END --

    float move = Input.GetAxis ("Horizontal");

    anim.SetFloat ("Speed", Mathf.Abs (move));

    rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);

    if (move > 0 && !facingRight)
            Flip ();
    else if (move < 0 && facingRight)
            Flip ();
}

【讨论】:

猜你喜欢
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
相关资源
最近更新 更多