unity3d 字符破折号功能不工作- Unity

kgsdhlau  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(229)

我正在开发我的独立游戏,我创建了一个破折号功能。在一个教程后重组了我的代码,我发现破折号已经停止工作。
代码如下:

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

       
}

在名为“DashCo”的IEEnumerator中,错误位于“if facingRight”if和else语句中。前面几行代码中的动画可以正确播放。前面,我甚至在if语句中添加了Debug.log(),以检查它是否被调用。控制台打印的日志,这意味着我的bug在:
theRB.velocity = new Vector2(dashingPower * Time.deltaTime, 0f);
以及else语句中的变量。当我在检查器中查看玩家的刚体时,有时我会看到一个简短的浮点数出现在再次变成0之前。
有人能帮我吗?

z9smfwbn

z9smfwbn1#

您有Update的登记

if (isDashing)
{
    return;
}

你没有***对FixedUpdate进行这样的检查,尽管你有多个地方(Move()),其中

theRB.velocity

也可以修改并可能设置为0

ev7lccsx

ev7lccsx2#

感谢https://stackoverflow.com/users/7111561/derhugo帮助我解决这个问题。当进入检查器时,我意识到当shutter时,“canMove”变量仍然被设置为true,而它应该被设置为false。这是无视了刚体的速度,沿着还利用了时间增量时间的冲击力,让玩家的速度慢了不少。

相关问题