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 JohnnyChef · Jan 21, 2015 at 02:23 PM · c#movementforce

Simple 2-Button-Mash script

Hello everyone, since im fairly new to unity, i am having big problems comming up with a solution for my problem. It's a simple 100-m-dash game. So by repeatedly pressing 2 buttons ("left" and "right") you gain momentum. The faster you mash the correct buttons, the faster you get. But pressing the wrong button slows you down... Pressing nothing does nothing.

Here's the Code:

 public class PlayerMovement : MonoBehaviour {
 
     Rigidbody PlayerRigidbody;
     int succRep = 0;
     float reqInput = -1f;
     Vector3 movement;
 
     void Awake(){
         PlayerRigidbody = GetComponent<Rigidbody> ();
         Debug.Log (reqInput);
         }
         
     void FixedUpdate()
     {
         Dash (); 
     }
 
     void Dash()
     {    
           float v = Input.GetAxisRaw ("Horizontal");
         //Debug.Log (v);
         if (v == reqInput && Input.GetButtonDown("Horizontal")) 
         {
             //Debug.Log (v);
             //Debug.Log ("SuccRep="+succRep);
             succRep++;
             PlayerRigidbody.AddForce(new Vector3(1f,0f,0f)*succRep);
             reqInput = reqInput * -1;
 
         } else {
             if(v == 0)
             {
             } 
             else {
                 if (v == (reqInput*-1) && Input.GetButtonDown("Horizontal"))
                 {
                     PlayerRigidbody.AddForce(new Vector3(-1f*succRep/2,0f,0f));
                     succRep = 0;
                 }
             }
         }
     }

}

So the problem i am having is: that the required Input ("reqInput") isn't alternating correctly. And i think it is because the game isn't properly checking the input given.

A big thank you 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
0

Answer by Mmmpies · Jan 21, 2015 at 03:35 PM

So GetAxisRaw it either -1, 0 or 1 as I understand it

 // At the top
 private bool reqInput = false;
 
 
 
 void Dash()
 {
     int v = Input.GetAxisRaw("Horizontal");
 
     if((v == -1 && !reqInput) || (v = 1 && reqInput)
     {
          succRep++;
          reqInput = !reqInput
          PlayerRigidbody.AddForce(new Vector3(1f,0f,0f)*succRep);
     }
     else if((v == -1 && reqInput) || ( v = 1 && !reqInput))
     {
         PlayerRigidbody.AddForce(new Vector3(-1f*succRep/2,0f,0f));
         succRep = 0;
     }
 }


I've not tested the code though, and you might want to record the first movement and set the bool accordingly.

I think it'll still have issues as a frame is normally very fast so it's likely you'll still be pressing the same direction on the next frame. You could maybe set a float = Time.time + 0.3f and only call Dash is Time.time is greater than that value.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How To Accelerate/Decelerate Gradually With Torque GetAxis 1 Answer

Roll a ball camera problem? 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