我的冲刺不会移动角色,但所有调试都有效

问题描述 投票:0回答:1
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;

public class PlayerController : MonoBehaviour
{
    // Input and movement variables
    private Vector2 direction;
    private Rigidbody2D rb;
    private PlayerControls controls;

    // Dash variables
    private float DashTimeLeft;
    private float LastImageExp;
    private float LastDash = -100;
    private bool isDashing;

    // Player movement parameters
    public float speed;
    public Animator animator;
    public Transform weaponParent;

    // Dash parameters
    public float DashTime;
    public float DashSpeed;
    public float DistanceBetweenImages;
    public float DashCooldown;

    private void Awake()
    {
        // Enable input controls and touch support
        controls = new PlayerControls();
        controls.Enable();
        EnhancedTouchSupport.Enable();
    }

    private void OnEnable()
    {
        // Register input callbacks
        controls.Player.Move.performed += ctx => direction = ctx.ReadValue<Vector2>();
        controls.Player.Move.canceled += ctx => direction = Vector2.zero;
        controls.Player.Dashing.performed += ctx => Dash();
    }

    private void OnDisable()
    {
        // Unregister input callbacks on disable
        controls.Player.Move.performed -= ctx => direction = ctx.ReadValue<Vector2>();
        controls.Player.Move.canceled -= ctx => direction = Vector2.zero;
    }

    private void Start()
    {
        // Initialize Rigidbody and Animator components
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        // Normalize input direction
        direction.Normalize();

        // Set animator parameters
        animator.SetFloat("Horizontal", direction.x);
        animator.SetFloat("Vertical", direction.y);
        animator.SetFloat("Speed", direction.magnitude);

        // Move character, set blend tree, rotate weapon, and check dash
        MoveCharacter();
        SetBlendTree();
        RotateWeapon();
        CheckDash();
    }

    private void MoveCharacter()
    {
        // Move character based on input direction unless dashing
        if (!isDashing)
        {
            rb.velocity = direction * speed;
        }
        else
        {
            rb.velocity = Vector2.zero; // Stop regular movement during dash
        }
    }

    private void SetBlendTree()
    {
        // Set animator parameters for blend tree based on input direction
        if (direction.magnitude > 0)
        {
            animator.SetInteger("LastDirection", GetDirectionCode());
            animator.SetBool("IsWalking", true);
        }
        else
        {
            animator.SetBool("IsWalking", false);
        }
    }

    private int GetDirectionCode()
    {
        // Determine and return the direction code based on input angle
        float x = direction.x;
        float y = direction.y;
        float northAngleThreshold = Mathf.PI / 3f;

        float angle = Mathf.Atan2(y, x);

        if (angle > -northAngleThreshold && angle < northAngleThreshold)
        {
            return 3; // 3 - North
        }
        else if (angle > northAngleThreshold && angle < Mathf.PI - northAngleThreshold)
        {
            return 1; // 1 - East
        }
        else if (angle < -northAngleThreshold && angle > -Mathf.PI + northAngleThreshold)
        {
            return 2; // 2 - West
        }
        else
        {
            return 4; // 4 - South
        }
    }

    private void RotateWeapon()
    {
        // Rotate weapon based on input direction
        if (weaponParent != null && direction.magnitude > 0)
        {
            float angle = Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg;
            weaponParent.rotation = Quaternion.Euler(0f, 0f, angle);
        }
    }

    public void Dash()
    {
        // Trigger dash if cooldown is over
        if (Time.time >= (LastDash + DashCooldown))
        {
            AttemptToDash();
            Debug.Log("Dash button pressed");
        }
        else
        {
            isDashing = false;
        }
    }

    private void AttemptToDash()
    {
        // Start dash, set dash parameters, and create afterimage
        isDashing = true;
        DashTimeLeft = DashTime;
        LastDash = Time.time;

        PlayerAfterImagePool.Instance.GetFromPool();
        LastImageExp = transform.position.x;
    }

    private void CheckDash()
    {
        // Check and perform dash if active
        Debug.Log("Checking Dash");

        if (isDashing)
        {
            Debug.Log("Dashing");

            if (DashTimeLeft > 0)
            {
                // Move character during dash and create afterimages
                rb.velocity = new Vector2(DashSpeed * direction.x, rb.velocity.y);
                DashTimeLeft -= Time.deltaTime;

                rb.MovePosition(rb.position + new Vector2(DashSpeed * direction.x, 0) * Time.deltaTime);

                if (Mathf.Abs(transform.position.x - LastImageExp) > DistanceBetweenImages)
                {
                    PlayerAfterImagePool.Instance.GetFromPool();
                    LastImageExp = transform.position.x;
                }
            }

            if (DashTimeLeft <= 0)
            {
                // End dash when time is up
                isDashing = false;
                Debug.Log("Dash finished");
            }
        }
    }
}

PlayerController
脚本提供的代码可处理玩家移动、冲刺输入和冲刺视觉效果。它利用 Unity 的输入系统,并具有冲刺能力的冷却时间。此外,该脚本还包含动画混合树、武器旋转以及冲刺过程中残像的创建。

c# unity-game-engine 2d d
1个回答
0
投票

当你的角色冲刺时,你将速度设置为 0 吗?这可能就是为什么角色在破折号期间不移动的原因

    // Move character based on input direction unless dashing
    if (!isDashing)
    {
        rb.velocity = direction * speed;
    }
    else
    {
        rb.velocity = Vector2.zero; // Stop regular movement during dash
    }
© www.soinside.com 2019 - 2024. All rights reserved.