- Home /
How do I set direction of player dash movement? Currently dashes to exact mouse position.
I have a dash ability on my player controller that so far works, but not exactly as intended. The player currently dashes to the exact mouse position not just that direction. Meaning, if my mouse is close to the character and I dash the dash is tiny, if the mouse is far away the dash length is huge. I want the player to dash in the mouse direction not the exact position.
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EasyDash : MonoBehaviour
{
private Rigidbody2D rb;
public bool notDashing;
public float dashSpeed = 25.0f;
public float dashCooldown = 10;
public float dashTimer = 10;
public Camera cam;
Vector2 mousePos;
// Use this for initialization
void Start()
{
notDashing = true;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (notDashing)
{
if (Input.GetMouseButtonDown(1))
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
transform.position = mousePos * dashSpeed;
notDashing = false;
dashTimer = 0.0f;
}
}
dashTimer += Time.deltaTime; //Makes timer increase
if (dashTimer >= dashCooldown)
{
dashTimer = dashCooldown; //Stops the timer
}
}
void FixedUpdate()
{
if (dashTimer >= dashCooldown)
notDashing = true;
}
}
Any help would be greatly appreciated. I feel like I have tried everything and already spent a few days reading through numerous forum posts.
Answer by TreyH · Sep 20, 2019 at 01:40 PM
As pointed out by s_awali, you want the direction instead of the position. You can get that by taking their differences though:
if (Input.GetMouseButtonDown(1))
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
var direction = (mousePos - this.transform.position);
transform.position += direction * dashSpeed;
notDashing = false;
dashTimer = 0.0f;
}
For a more complete example, you can try using a coroutine to help with stateful things like dashing:
using System.Collections;
using UnityEngine;
public class Dash : MonoBehaviour
{
public bool dashing;
public float dashDistance = 5.0f;
public float dashDuration = 0.15f;
public Camera cam;
// Update is called once per frame
void Update ()
{
if (!dashing)
{
if (Input.GetMouseButtonDown (1))
{
var mousePos = cam.ScreenToWorldPoint (Input.mousePosition);
var direction = (mousePos - this.transform.position);
// Z-component won't matter in 2D. If you want to only dash in
// the X-direction (HK without Dashmaster), then you'd also set
// your y-component to zero.
direction.z = 0;
// Making sure we have a reasonable vector here
if (direction.magnitude >= 0.1f)
{
// Don't exceed the target, you might not want this
this.StartCoroutine (this.DashRoutine (direction.normalized));
}
}
}
}
IEnumerator DashRoutine (Vector3 direction)
{
// Account for some edge cases
if (this.dashDistance <= 0.001f)
yield break;
if (this.dashDuration <= 0.001f)
{
this.transform.position += direction * this.dashDistance;
yield break;
}
// Update our state
this.dashing = true;
var elapsed = 0f;
var start = this.transform.position;
var target = this.transform.position + this.dashDistance * direction;
// There are a few different ways to do this, but I've always preferred
// Lerp for things that have a fixed duration as the interpolant is clear
while (elapsed < this.dashDuration)
{
var iterTarget = Vector3.Lerp (start, target, elapsed / this.dashDuration);
this.transform.position = iterTarget;
yield return null;
elapsed += Time.deltaTime;
}
// Snap there when we finish then update our state
this.transform.position = target;
this.dashing = false;
}
}
Which gives the following:
Thank you very much! The full example works like a charm. It does pass through colliders but that is another problem to figure out later at another time :) Huge thanks to both you and s_awali for the help!
Answer by s_awali · Sep 19, 2019 at 07:39 AM
By using (mousePos - transform.position).normalized
you'll get the direction from the player to the mousePos. Hope this will help :)
Thank you for the idea! I tried plugging that in here:
transform.position = mousePos.normalized * dashSpeed;
but it is making that character dash back to the origin (0, 0) spot. I read this is in the documentation "If the vector is too small to be normalized a zero vector will be returned." so maybe my distance is too small to be recognized :(
Not giving up though! Going to keep trying more stuff.
It seems like everything I try just sends the character back to the origin point, so frustrating! I just plugged this in and it is also sending the controller back to 0,0
rb.AddForce(transform.forward * dashSpeed * Time.deltaTime);
I just don't get why this isn't working ><
$$anonymous$$y bad, you should not normalize the mouse vector, but the direction from the player to the mouse position. If you do the following your dash should work fine : transform.position += (mousePos - transform.position).normalized * dashSpeed;
Your answer
![](https://koobas.hobune.stream/wayback/20220612212344im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How Do I Make A Dash&Attack Move For My Top-DownRPG 2D? 0 Answers
AddForce problem axis X 1 Answer
Spawning 2 bullets when I want 1 2 Answers
2d Top Down Shooter Jerky Movement 0 Answers
Able to dash in the air but NOT on the floor/standing tile 1 Answer