i wanna make my charactor dash to the mouse direction,how to dash to the mouse direction

i wanna make my charactor dash to direction of my mouse
how should i do it
here is my code
please help me:)

using System.Collections;
using UnityEngine;

public class PlayerGrappling : MonoBehaviour
{
private Rigidbody2D _rigidbody2D;
private LineRenderer _lineRenderer;
private Transform _closest;
private SpringJoint2D _springJoint2D;
private Collider2D collision;
private SpriteRenderer spriteRenderer;

private float _maxSpeed = 30f;

public GameObject hitEffect;
public GameObject dieEffect;
public GameObject player;
public GameObject Diepoint;
public CircleCollider2D CCollider;

public float forceAmount;
private void Start()
{
    _rigidbody2D = GetComponent<Rigidbody2D>();
    _lineRenderer = GetComponent<LineRenderer>();
    collision = GetComponent<Collider2D>();
    spriteRenderer = GetComponent<SpriteRenderer>();
    _lineRenderer.positionCount = 0;
}

private void Update()
{
    if (Input.GetMouseButtonDown(1))
    {
    dash();
    }

    if (Input.GetMouseButton(0))
    {
        // draw line from player to hinge
        _lineRenderer.SetPosition(0, transform.position);
        _lineRenderer.SetPosition(1, _closest.position);
        _rigidbody2D.AddForce(transform.forward * forceAmount, ForceMode2D.Impulse);

        if (_springJoint2D.distance > 3)
        {
            _springJoint2D.distance -= 0.01f;
        }
    }

    if (Input.GetMouseButtonUp(0))
    {
        // remove line and hinge
        _lineRenderer.positionCount = 0;
        _springJoint2D.connectedBody = null;
    }
}

private void FixedUpdate()
{

    if (_rigidbody2D.velocity.magnitude > _maxSpeed)
    {
        _rigidbody2D.velocity = _rigidbody2D.velocity.normalized * _maxSpeed;
    }
}

Transform GetClosestHinge(GameObject[] hinges)
{
    Transform closest = null;
    var closestDistanceSqr = Mathf.Infinity;
    var currentPosition = transform.position;
    foreach (var potentialTarget in hinges)
    {
        var directionToTarget = potentialTarget.transform.position - currentPosition;
        var dSqrToTarget = directionToTarget.sqrMagnitude;
        if (!(dSqrToTarget < closestDistanceSqr)) continue;
        closestDistanceSqr = dSqrToTarget;
        closest = potentialTarget.transform;
        CCollider.enabled = true;
    }

    return closest;
}

void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.tag == "unable")
    {
        blue();

    }

    if (collision.gameObject.tag == "Die") 
    {
        Die();
        _lineRenderer.positionCount = 0;
        _springJoint2D.connectedBody = null;
    }

}
void Die()
{
      Debug.Log("Die");
        Destroy(player);
    Instantiate(dieEffect, collision.transform.position, Quaternion.identity);
}
void blue()
{
        spriteRenderer.color = new Color(1, 1, 1, 0.4f);
        _springJoint2D.enabled = false;
}
void dash() {

 } 

}

,i wanna add the dash abillity to my player
my player charactor is unable to move because i didnt code it to
so i just wanna make it dash to the mouse pointer’s direction
my code is this

using System.Collections;
using UnityEngine;

public class PlayerGrappling : MonoBehaviour
{
private Rigidbody2D _rigidbody2D;
private LineRenderer _lineRenderer;
private Transform _closest;
private SpringJoint2D _springJoint2D;
private Collider2D collision;
private SpriteRenderer spriteRenderer;

private float _maxSpeed = 30f;

public GameObject hitEffect;
public GameObject dieEffect;
public GameObject player;
public GameObject Diepoint;
public CircleCollider2D CCollider;

public float forceAmount;
private void Start()
{
    _rigidbody2D = GetComponent<Rigidbody2D>();
    _lineRenderer = GetComponent<LineRenderer>();
    collision = GetComponent<Collider2D>();
    spriteRenderer = GetComponent<SpriteRenderer>();
    _lineRenderer.positionCount = 0;
}

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        // get the closest hinge
        _closest = GetClosestHinge(GameObject.FindGameObjectsWithTag("hinge"));
        // attach joint to it
        _springJoint2D = _closest.GetComponent<SpringJoint2D>();
        _springJoint2D.connectedBody = _rigidbody2D;
        // set line renderer position count
        _lineRenderer.positionCount = 2;
    }

    if (Input.GetMouseButton(0))
    {
        // draw line from player to hinge
        _lineRenderer.SetPosition(0, transform.position);
        _lineRenderer.SetPosition(1, _closest.position);
        _rigidbody2D.AddForce(transform.forward * forceAmount, ForceMode2D.Impulse);

        if (_springJoint2D.distance > 3)
        {
            _springJoint2D.distance -= 0.01f;
        }
    }

    if (Input.GetMouseButtonUp(0))
    {
        // remove line and hinge
        _lineRenderer.positionCount = 0;
        _springJoint2D.connectedBody = null;
    }
}

private void FixedUpdate()
{

    if (_rigidbody2D.velocity.magnitude > _maxSpeed)
    {
        _rigidbody2D.velocity = _rigidbody2D.velocity.normalized * _maxSpeed;
    }
}

Transform GetClosestHinge(GameObject[] hinges)
{
    Transform closest = null;
    var closestDistanceSqr = Mathf.Infinity;
    var currentPosition = transform.position;
    foreach (var potentialTarget in hinges)
    {
        var directionToTarget = potentialTarget.transform.position - currentPosition;
        var dSqrToTarget = directionToTarget.sqrMagnitude;
        if (!(dSqrToTarget < closestDistanceSqr)) continue;
        closestDistanceSqr = dSqrToTarget;
        closest = potentialTarget.transform;
        CCollider.enabled = true;
    }

    return closest;
}

void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.tag == "unable")
    {
        blue();

    }

    if (collision.gameObject.tag == "Die") 
    {
        Die();
        _lineRenderer.positionCount = 0;
        _springJoint2D.connectedBody = null;
    }

}
void Die()
{
      Debug.Log("Die");
        Destroy(player);
    Instantiate(dieEffect, collision.transform.position, Quaternion.identity);
}
void blue()
{
        spriteRenderer.color = new Color(1, 1, 1, 0.4f);
        _springJoint2D.enabled = false;
}

}

Hi! The simplest way to do something like this would be to just get the positions of both the player and mouse, and use some basic vector math to get the direction out of those. That might look a little like this:

public void Dash()
{
    float dashSpeed = 10f;

    Vector3 playerScreenPosition = Camera.main.WorldToScreenPoint(player.transform.position);
    Vector3 mouseScreenPosition = Input.mousePosition;

    Vector3 playerToMouseVector = (mouseScreenPosition - playerScreenPosition).normalized;
    
    _rigidbody2D.velocity = playerToMouseVector * dashSpeed;
}

The above snippet normalizes the playerToMouseVector to make the player always dash with the same speed, regardless of where you click. (if clicking further away should make you dash further, just remove the ‘.normalized’ part.)

You will probably also want to mess around with the friction on the RigidBody to control how quickly the player should slow down and stop, as well as the dashSpeed in the above function.

Hope this helps :slight_smile: