首页 > 解决方案 > 不稳定的球员运动

问题描述

无论出于何种原因,我的玩家移动每秒或每隔一秒都会卡顿,我确信这与我的玩家移动代码使用了错误的更新方法有关。

这是我的玩家移动代码

  [SerializeField] private LayerMask groundLayerMask;
public float speed;
public float Jump;
public sword swordScript;
public GameObject swordSprite;
private float move;
private Rigidbody2D rb;
private BoxCollider2D boxCollider2d;
private bool facingRight;
public SpriteRenderer spr;
public Animator PlayerAnims;
public bool movementAllowed;
public float knockback;


void Awake()
{
    boxCollider2d = GetComponent<BoxCollider2D>();
    rb = GetComponent<Rigidbody2D>();
    facingRight = true;
    spr = GetComponent<SpriteRenderer>();
}
// Start is called before the first frame update
void Start()
{
    boxCollider2d = GetComponent<BoxCollider2D>();
    rb = GetComponent<Rigidbody2D>();
    facingRight = true;
    spr = GetComponent<SpriteRenderer>();
}



// Update is called once per frame

void FixedUpdate()
{
    if(movementAllowed == true)
    {
        rb.velocity = new Vector2(move * speed, rb.velocity.y);
        move = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(move * speed, rb.velocity.y);
        if (isGrounded() && Input.GetButtonDown("Jump"))
        {
            rb.AddForce(new Vector2(rb.velocity.x, Jump));
        }
    }
}


void Update()
{
    if (movementAllowed == true)
    {
        Flip(move);

        if (move == 0)
        {
            PlayerAnims.SetBool("isRunning", false);
        }
        else
        {
            PlayerAnims.SetBool("isRunning", true);
        }
    }


}


private bool isGrounded()
{
    float extraHeightText = .1f;
    RaycastHit2D raycasthit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down,  extraHeightText, groundLayerMask);
    Color rayColour;
    if (raycasthit2d.collider != null)
    {
        rayColour = Color.green;
        PlayerAnims.SetBool("isJumping", false);
    }
    else
    {
        rayColour = Color.red;
        PlayerAnims.SetBool("isJumping", true);

    }
    Debug.DrawRay(boxCollider2d.bounds.center + new Vector3(boxCollider2d.bounds.extents.x, 0), Vector2.down * (boxCollider2d.bounds.extents.y + extraHeightText), rayColour);
    Debug.DrawRay(boxCollider2d.bounds.center - new Vector3(boxCollider2d.bounds.extents.x, 0), Vector2.down * (boxCollider2d.bounds.extents.y + extraHeightText), rayColour);
    Debug.DrawRay(boxCollider2d.bounds.center - new Vector3(boxCollider2d.bounds.extents.x, boxCollider2d.bounds.extents.y + extraHeightText), Vector2.right * (boxCollider2d.bounds.extents.x), rayColour);

    return raycasthit2d.collider != null;
}
private void Flip(float move)
{
    if (move > 0 && !facingRight || move < 0 && facingRight)
    {
        facingRight = !facingRight;

        Vector3 theScale = transform.localScale;

        theScale.x *= -1;

        transform.localScale = theScale;

        if (swordScript.isFollowing == true)
        {
            Vector3 swordScale = swordSprite.transform.localScale;

            swordScale.x *= -1;

            swordSprite.transform.localScale = swordScale;
        }
    }
}

标签: c#unity3dgame-development

解决方案


我试过你的脚本,我想我几乎看不到那种口吃,但有一件事似乎可以解决它。

您无缘无故地在代码中有两次此特定行

rb.velocity = new Vector2(move * speed, rb.velocity.y);

只需删除第一个。


推荐阅读