角色被困在墙上

时间:2014-02-05 20:04:16

标签: unity3d unityscript

我最近开始使用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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

摩擦力设定为0应该没问题。角色边缘距离块的边缘有多远?您可以尝试相对于角色(特别是宽度)缩小圆形对撞机的尺寸。如果所有其他物理工作都正常,则可能与圆形对撞机的定位或尺寸有关。

答案 1 :(得分:0)

在这里,我的修复对我来说非常有用。 我刚刚检查过玩家是否与不是地面的东西相撞。在这种特殊情况下,我禁用播放器(用户输入)移动:

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 ();
}