Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by UbaNess · Sep 19, 2019 at 06:29 AM · 2d-physicstop down shooterdash

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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:
alt text


dash.gif (230.7 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UbaNess · Sep 21, 2019 at 02:05 AM 0
Share

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!

avatar image
1

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 :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UbaNess · Sep 19, 2019 at 07:44 PM 0
Share

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.

avatar image UbaNess · Sep 19, 2019 at 11:34 PM 0
Share

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 ><

avatar image s_awali UbaNess · Sep 20, 2019 at 08:36 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How Do I Make A Dash&Attack Move For My Top-DownRPG 2D? 0 Answers

AddForce problem axis X 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges