Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 muhammadaliawan96 · Oct 18, 2020 at 06:57 PM · attackdashsprintattackingsprinting

unity 2d platformer dash attack.

hello everyone, i am working on a 2d platformer and have done a lot of work regarding player movement (i aint a programmer, i am learning c# as i am working, changed my player movement script 4 times by now because of learning and experimenting and i am happy with it). currently i am stuck at dash attack. tried various youtube tutorials but cant seem to help. what i am looking for is, player facing left or right, when the dash key is pressed, it should do a sprint/dash for small distance in that direction and kill/damage enemies coming its way, the sprint should be available after set time only so it cannot be spammed. if anyone of you have any spare script or can point me at some free scripts library or anything, that would be great. it is my 1st time ever i am working on a game dev project, all because of my passion. thank you already in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by dignus1993 · Oct 18, 2020 at 11:06 PM

In my code I have an object Player with a script PlayerMovement.cs attached to it. The enemy has a script EnemyHealthManager.cs with a public variable health that is equal to his actual healthpoint.

In my script PlayerMovement you have a function dash function, in order to adjust the parameter of your script change these variables:

speed is the speed of the player when he moves

dashSpeed is the speed of the dash;

dashCountThreshold is the treshold that currentDashSpeed has to underpass in order to stop dashing (dashCountThreshold must be < dashSpeed);

cooldownDash is the time to pass before pressing again the dodge button;


 public class PlayerMovement : MonoBehaviour
 {
     private Rigidbody2D rb;
     private float dashTimer;
     private Vector3 moveDirection;
     private Vector3 lastMoveDirection;
     private Vector3 dashDirection;
     private float currentDashSpeed;
     public float speed;
     public float dashSpeed;
     public float dashCountThreshold;
     public float cooldownDash;
 
     public enum State
     {
         Normal,
         Dashing,
     }
     private State state;
 
     void Start()
     {
         speed = 10;
         dashSpeed = 100;
         dashCountThreshold = 60;
         cooldownDash = 50;
         dashTimer = 0;
         rb = GetComponent<Rigidbody2D>();
         state = State.Normal;
     }
     
     void FixedUpdate()
     {
         dashTimer--;
         switch (state)
         {
             case State.Normal:
                 moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical")).normalized;
                 if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
                 {
                     lastMoveDirection = moveDirection;
                     rb.velocity = moveDirection * speed;
                 }
 
                 if (Input.GetButtonDown("Dodge"))
                 {
                     if (dashTimer <= 0)
                     {
                         dashDirection = lastMoveDirection;
                         currentDashSpeed = dashSpeed;
                         dashTimer = cooldownDash;
                         state = State.Dashing;
                     }
                 }
                 break;
 
             case State.Dashing:
                 currentDashSpeed -= currentDashSpeed * Time.deltaTime;
                 rb.velocity = dashDirection * currentDashSpeed;
                 if (currentDashSpeed < dashCountThreshold)
                 {
                     state = State.Normal;
                     dashTimer = cooldownDash;
                 }
                 break;
         }
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag.Equals("Enemy"))
             other.gameObject.GetComponent<EnemyHealthManager>().health -= 1;
     }
 }


Comment
Add comment · 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

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

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

Timed Attack System 1 Answer

How can I make an event happen every 2 seconds in the OnTriggerStay void? 2 Answers

How do I fix an issue with my enemy instantly dying? 1 Answer

How to do a "dash"-movement, set distance on key-input? 0 Answers

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


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