Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 coolsor · Jan 11, 2020 at 04:41 AM · movementif-statementsdash

Dash Movement script works until if statement is added

This script works until the section of code I marked with a comment is added and i can't understand why... I'm thinking about reworking my basic movement controls to something more written out that i know will work but i'd like to have this more simple and graceful code more than something basic. If anyone can tell me what I'm doing wrong that'd be awesome!

 public class Player : MonoBehaviour
 {
 
     public float speed = 5f;
     public Rigidbody2D rb;
     [SerializeField] 
     private float dashMultiplier = 1f;
     private Vector3 tempVect;
     
     void Update()
     {
         /* if (Input.GetButton("Space"))
         {
             dashMultiplier = 1.3f;
         } */
 
         DashCheck(dashMultiplier);
 
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
 
         tempVect = new Vector2(h, v);
         tempVect = tempVect.normalized * speed * Time.deltaTime;
         rb.MovePosition(rb.transform.position + tempVect);
 
         if (Input.GetButtonUp("Up"))
         {
             rb.velocity = Vector3.zero;
         }
 
         else if (Input.GetButtonUp("Down"))
         {
             rb.velocity = Vector3.zero;
         }
 
         else if (Input.GetButtonUp("Left"))
         {
             rb.velocity = Vector3.zero;
         }
 
         else if (Input.GetButtonUp("Right"))
         {
             rb.velocity = Vector3.zero;
         }
 
         speed = 5f;
         dashMultiplier = 1f;
     }
 
     void DashCheck (float dashMultiplier)
     {
         speed = speed * dashMultiplier;
     }
 }
 
Comment
Add comment · Show 1
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 Feref2 · Jan 11, 2020 at 10:01 AM 1
Share

What´s exactly your undesired behavior?

1 Reply

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

Answer by LTonon · Jan 11, 2020 at 05:32 PM

This does not work because you are using GetButton to reference your Space key. GetButton returns true only if the virtual button with the given name is being pressed, and those buttons are defined at the InputManager in ProjectSettings. By default there is no virtual button with the name "Space", if you have created one with this name then the problem is something else, however, I think that if you change your condition inside the if to

 if (Input.GetKeyDown(KeyCode.Space))

it should compile correctly, but I still think your dash would be weird.


I recommended GetKeyDown since most dashes in games are done by just pressing the button once, but it seems like your code would only work if the player press and hold the space key (which a GetKey would help)... But in this way the player could use a dash all the time, if I understood it correctly.

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 coolsor · Jan 12, 2020 at 01:32 PM 0
Share

Thanks so much for the help, this worked and you’re right the dash is really really weird. I’ve gotta tune it up and change some stuff but thanks for help getting it up and running!

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

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

Smooth dashing issues 0 Answers

Object doesn't move 2 Answers

Dashing with rigidbody2D not working right 2 Answers

Dash and player script not working together! 0 Answers

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