如何修复跳跃时冲刺无法正常工作的问题,以及如何添加冷却时间?

How can I fix my dash not working properly when jumping and how can I add a cooldown?

提问人:HarrisonO 提问时间:7/22/2020 最后编辑:RafalonHarrisonO 更新时间:7/22/2020 访问量:43

问:

最近我一直在一起在网上找到的弗兰肯斯坦代码,我正在努力正确实现破折号。在目前的状态下,每当我的角色跳跃然后使用他的冲刺时,他就会被卷入空中,就好像冲刺没有移动他一样。显然这是故意的,我想知道如何解决这个问题。另外,我怎么能在我的冲刺能力中加入一个冷却系统。我尝试过使用各种来源作为冷却脚本,但它们似乎总是有缺点。 提前致谢!

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    //Player Movement
    public float speed;
    public float jumpForce;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;
    public float dashSpeed;
    public float startDashTime;
    
    private float timeStamp = 0;

    public Animator animator;

    private Rigidbody2D rb;
    private float moveInput;
    private bool isGrounded;
    private float jumpTimeCounter;
    public float jumpTime;
    private bool isJumping;
    private float dashTime;
    public int direction;
    
    void Start()
    {
        animator.GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        dashTime = startDashTime;
    }

    void FixedUpdate()
    {
        moveInput = Input.GetAxisRaw("Horizontal");
        if (direction < 1)
            rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    }
    void Update()
    {
        // Moving
        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

        if (moveInput > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            animator.SetBool("Moving", true);
        }
        else if (moveInput < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            animator.SetBool("Moving", true);
        }
        else
        {
            animator.SetBool("Moving", false);
        }
        // Jumping
        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("IsJumping");
            isJumping = true;
            jumpTimeCounter = jumpTime;
            rb.velocity = Vector2.up * jumpForce;
        }

        if (Input.GetKey(KeyCode.Space) && isJumping == true)
        {
            if (jumpTimeCounter > 0)
            {
                rb.velocity = Vector2.up * jumpForce;
                jumpTimeCounter -= Time.deltaTime;
            }
            else
            {
                isJumping = false;
            }
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            isJumping = false;
        }
        if (isGrounded == false)
        {
            animator.SetBool("Grounded", false);
        }
        if (isGrounded == true)
        {
            animator.SetBool("Grounded", true);
        }
    
        // Dashing
        if (direction == 0)
        {
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                if ((transform.rotation.eulerAngles.y == 180))
                {
                    Dashleft();
                }
                else
                {
                    DashRight();
                }
            }
        }
        else
        {
            if (dashTime <= 0)
            {
                direction = 0;
                dashTime = startDashTime;
                rb.velocity = Vector2.zero;
            }
            else
            {
                dashTime -= Time.deltaTime;
            }
        }
        void Dashleft()
        {
            direction = 1;
            rb.velocity = Vector2.left * dashSpeed;
        }
        void DashRight()
        {
            direction = 1;
            rb.velocity = Vector2.right * dashSpeed;
        }
    }
}
C# Unity-游戏引擎

评论

0赞 Rafalon 7/22/2020
的值是多少?startDashTime
0赞 HarrisonO 7/22/2020
startDashTime确定破折号将持续的时间。
0赞 Rafalon 7/22/2020
是的,我理解这一点,但我在您的代码中没有看到任何地方,所以如果您不在任何地方设置它,它的值将是,我真的不明白您希望它如何工作startDashTime = ...;0
0赞 HarrisonO 7/22/2020
它是公开的,所以我在检查器中确定值。通常其 0.2
0赞 Rafalon 7/22/2020
好的,所以第一帧当你冲刺时,你设置了,但是下一帧,你进入跳跃块并将其设置回去,所以你只“冲刺”了 1 帧(你不再冲刺了,因为你设置了第一次,所以你不输入第二次)rb.velocity = Vector2.[left/right] * dashspeedrb.velocity = Vector2.up * jumpForcedirection = 1if(direction == 0)

答: 暂无答案