Character Dash Function Is Not Working – Unity

huangapple go评论52阅读模式
英文:

Character Dash Function Is Not Working - Unity

问题

在名为“DashCo”的IEnumerator中,问题出现在“if facingRight”和“else”语句中。前面的代码中的动画播放得正确。之前,我甚至在if语句中放入了一个Debug.Log()来检查它是否被调用。日志在控制台中打印出来,这意味着我的问题出现在以下代码行中:

theRB.velocity = new Vector2(dashingPower * Time.deltaTime, 0f);

以及else语句中的变体。当我查看玩家的Rigidbody时,有时会在0之前出现一个短暂的浮点数,然后再次变成0。

有人可以帮我解决这个问题吗?

英文:

I'm developing my indie game and I created a dash function. After restructuring my code after a tutorial, I found that the dash has stopped working.

Here is the code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public static Movement instance;
    public Rigidbody2D theRB;
    Animator anim;
    SpriteRenderer theSR;

    [SerializeField] float wallCheckDistance, groundPointRadius;
    [SerializeField] public Transform wallCheck;

    [Header("Movement & Direction Info")]
    public float moveSpeed;
    public float jumpForce;
    public float dashingPower = 10f;
    public float dashingTime = .2f;
    public float dashingCooldown = 1f;
    public bool canDash = true;
    public bool canMove = true;
    bool isDashing;
    public bool doubleJumpActive;
    float knockBackCounter;
    float movingInput;

    int facingDirection = 1;
    bool facingRight = true;

    [Header("Collision & Detection Info")]
    public LayerMask whatIsGround;
    public Transform groundCheckPoint;
    bool isGrounded;
    bool isWallDetected;  
    bool crouching;
    bool canWallSlide;
    bool isWallSliding;
    bool canWallJump;
    public float knockBackLength;
    public float knockBackForce;
    [SerializeField] private Vector2 wallJumpDirection;

    void Awake(){
        instance = this;
        anim = GetComponent<Animator>();
        theSR = GetComponent<SpriteRenderer>();
    }

    private void FixedUpdate()
    {
        if (isGrounded)
        {
            canMove = true;
            doubleJumpActive = true;

            if (Input.GetAxis("Vertical") < 0)
            {
                crouching = true;
                anim.SetBool("crouch", true);
            }
            else
            {
                crouching = false;
                anim.SetBool("crouch", false);
            }
        }

        if (isWallDetected && canWallSlide && !isGrounded)
        {
            isWallSliding = true;
            canWallJump = true;
            theRB.velocity = new Vector2(theRB.velocity.x, theRB.velocity.y * .1f);
        }

        else if (isWallDetected && isGrounded)
        {
            Move();
        }
        else if(!isWallDetected)
        {
            isWallSliding = false;
            canWallJump = false;
            Move();
        }
    }

    private void Move()
    {
        if (canMove)
        {
            theRB.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y);
        }
    }

    void Update()
    {
        velocity = theRB.velocityX;

        if (isDashing)
        {
            return;
        } 

        if (knockBackCounter <= 0) {

            CheckInput();

            CollisionCheck();

            if (Input.GetKeyDown(KeyCode.Z) && canDash && !crouching)
            {
                StartCoroutine(DashCo());
            }
        }

        else
        {
            knockBackCounter -= Time.deltaTime;

            if (facingRight)
            {
                theRB.velocity = new Vector2(-knockBackForce, theRB.velocity.y);
            }
            else
            {
                theRB.velocity = new Vector2(knockBackForce, theRB.velocity.y);
            }     
        }

        FlipController();
        AnimatorController();
        
    }

    public void KnockBack()
    {
        knockBackCounter = knockBackLength;
        theRB.velocity = new Vector2(0f, knockBackForce);
        anim.SetTrigger("hurt");
    }

    public IEnumerator DashCo()
    {
        if (PlayerStats.instance.currentEnergy >= (PlayerStats.instance.maxEnergy / 3) && Movement.instance.canDash)
        {
            anim.SetTrigger("dash");
            PlayerStats.instance.currentEnergy -= (PlayerStats.instance.maxEnergy / 3);
            EnergyBar.instance.SetEnergy(PlayerStats.instance.currentEnergy);

            canDash = false;
            isDashing = true;
            float originalGravity = theRB.gravityScale;
            theRB.gravityScale = 0f;

            if (facingRight)
            {
                theRB.velocity = new Vector2(dashingPower * Time.deltaTime, 0f);
            }

            else
            {
                theRB.velocity = new Vector2(-dashingPower * Time.deltaTime, 0f);
            }

            yield return new WaitForSeconds(dashingTime);
            theRB.gravityScale = originalGravity;
            isDashing = false;
            yield return new WaitForSeconds(dashingCooldown);
            canDash = true;
        }

        yield return new WaitForSeconds(.5f);
    }

    void CollisionCheck()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundPointRadius, whatIsGround);
        isWallDetected = Physics2D.Raycast(wallCheck.position, Vector2.right, wallCheckDistance, whatIsGround);

        if (!isGrounded && theRB.velocity.y < 0)
        {
            canWallSlide = true;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(groundCheckPoint.position, groundPointRadius);
        Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y, wallCheck.position.z));

    }

    void FlipController()
    {
        /* if(isGrounded & isWallDetected)
        {
            if (facingRight && movingInput > 0)
            {
                Flip();
            }

            else if (!facingRight && movingInput < 0)
            {
                Flip();
            }
        } */

        if(theRB.velocity.x > 0 && !facingRight)
            Flip();

        else if(theRB.velocity.x < 0 && facingRight)      
            Flip();
       
    }

    void Flip()
    {
        facingDirection = facingDirection * -1;
        facingRight = !facingRight;
        transform.Rotate(0, 180, 0);
    }

    void AnimatorController()
    {
        anim.SetBool("isGrounded", isGrounded);
        anim.SetFloat("moveSpeed", Mathf.Abs(theRB.velocity.x));
        anim.SetBool("isWallSliding", isWallSliding);
    }

    void CheckInput()
    {
        if (canMove)
        {
            movingInput = Input.GetAxisRaw("Horizontal");
        }

        if (Input.GetButtonDown("Jump") && !crouching)
        {
            if(isWallSliding && canWallJump)
            {
                anim.SetTrigger("jump");
                WallJump();
            }
            else if (isGrounded)
            {
                anim.SetTrigger("jump");
                Jump();
            }
            else if (doubleJumpActive)
            {          
                anim.SetTrigger("doubleJump");
                canMove = true;
                doubleJumpActive = false;
                Jump();           
            }

            canWallSlide = false;
        }

       
}

In the IEnumerator named "DashCo," the bug is in the "if facingRight" if and else statement. The animation in the earlier lines of code plays correctly. Earlier, I even put in a Debug.Log() in the if statement to check if it was even being called at all. The log printed in the console, which meant my bug is in:

theRB.velocity = new Vector2(dashingPower * Time.deltaTime, 0f);

and the variant in the else statement. When I look at the player's Rigidbody in the inspector, sometimes I see a brief float number appears on the before turning back into a 0 again.

Can anyone help me on this?

答案1

得分: 1

你在 Update 中有一个检查条件:

if (isDashing)
{
    return;
}

但是在 FixedUpdate 中没有这样的检查条件,而在其中有多个地方调用了 Move(),其中

theRB.velocity

也可能被修改并潜在地设为 0

英文:

You have a check in Update for

if (isDashing)
{
    return;
} 

you do not have such a check for FixedUpdate though where you have multiple places (Move()) where the

theRB.velocity

can be modified as well and potentially set to 0

huangapple
  • 本文由 发表于 2023年5月17日 08:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定