Need Help with Player Dash Mechanic in Unit

Hello everyone,

I’m new to Unity and currently working on a top-down shooter project. I’m having trouble implementing a dash mechanic for my player character. The dash is supposed to send the player in the direction of the mouse cursor when the “K” key is pressed, but it’s not functioning as expected.

Here’s the script I’m using for player movement:

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

public class Player_Movement : MonoBehaviour
{
    [Header("Player-Movement-Settings")]
    [SerializeField] private float PlayerSpeed = 5f; // Spieler Geschwindigkeit
    [HideInInspector] public bool allowRunning = true; // Erlaubt dem Spieler zu rennen
    [HideInInspector] public Vector2 movement;

    [Space] // Abstand

    [Header("Player-Dash-Settings")]
    [SerializeField] private float DashPower = 10f; // Stelle sicher, dass DashPower hoch genug ist
    [HideInInspector] public bool allowDash = true;

    [Header("Important-Referenzen")]
    private Rigidbody2D PlayerRb;
    private Animator PlayerAnimation;
    private Vector3 mousePosition;

    void Start()
    {
        PlayerRb = GetComponent<Rigidbody2D>(); // Getting Referenzen
        PlayerAnimation = GetComponent<Animator>();
    }

    void Update()
    {
        GetInputs(); // Eingaben nur in Update abfragen
        DashToMouse(); // Der Spieler kann Dashen
    }

    void FixedUpdate()
    {
        if (allowRunning)
        {
            MovePlayerMethod(); // Spieler bewegen
        }
    }

    // Eingaben bekommen
    private void GetInputs()
    {
        // Player Inputs
        movement.x = Input.GetAxis("Horizontal");
        movement.y = Input.GetAxis("Vertical");

        // Mausposition abrufen und in Weltkoordinaten umwandeln
        mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
        mousePosition.z = 0; // Setze Z-Koordinate für 2D

        // Debugging: Mausposition überprüfen
        Debug.Log($"Mouse Position: {mousePosition}");
    }

    // Spieler bewegen
    private void MovePlayerMethod()
    {
        Vector2 targetVelocity = movement.normalized * PlayerSpeed; // Zielgeschwindigkeit
        PlayerRb.MovePosition(PlayerRb.position + targetVelocity * Time.fixedDeltaTime); // Spieler bewegen

        if (movement.magnitude > 0) // Wenn der Spieler sich bewegt
        {
            PlayerAnimation.SetBool("isWalkingRight", movement.x > 0); // Überprüfen, ob nach rechts
            PlayerAnimation.SetBool("isWalkingLeft", movement.x < 0); // Überprüfen, ob nach links
            PlayerAnimation.SetBool("isWalkingBack", movement.y > 0); // Spieler läuft nach oben
        }
        else // Spieler steht still
        {
            PlayerAnimation.SetBool("isWalkingRight", false);
            PlayerAnimation.SetBool("isWalkingLeft", false);
            PlayerAnimation.SetBool("isWalkingBack", false); // Animation für oben zurücksetzen
        }
    }

    private void PlayerStand()
    {
        if (movement.magnitude == 0)
        {
            // Sofortiges Stoppen der Bewegung
            PlayerRb.velocity = Vector2.zero; // Setzt die Geschwindigkeit auf null
        }
    }

    private void DashToMouse()
    {
        if (PlayerRb != null && allowDash && Input.GetKeyDown(KeyCode.K))
        {
            // Richtung zur Maus berechnen
            Vector2 directionToMouse = (mousePosition - transform.position).normalized;

            // Debugging: Richtung und Dash-Kraft überprüfen
            Debug.Log($"Direction to Mouse: {directionToMouse}, Dash Power: {DashPower}");

            // Dash in Richtung der Maus mit DashPower
            PlayerRb.AddForce(directionToMouse * DashPower, ForceMode2D.Impulse);

            // Debugging: Überprüfen, ob Velocity angewendet wurde
            Debug.Log($"Player Velocity after Dash: {PlayerRb.velocity}");
        }
    }
}

Issues I’m Facing:

  1. The player is not dashing towards the mouse cursor when I press the “K” key.
  2. I have tested various aspects, including input detection, but nothing seems to resolve the issue.

Any help or suggestions would be greatly appreciated! Thank you!

The MovePosition in the MovePlayerMethod is always overriding the player’s velocity acquired from the DashToMouse.

Either switch to moving the player with AddForce in the MovePlayerMethod or disable the MovePlayerMethod while the player is dashing.

2 Likes