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 karim_fawaz · Feb 02, 2019 at 07:25 PM · guimovementunity 2dcar gamearcade

Building a 2D car game and want to increase speed while the button is being held down.

I want the button that I added (gas pedal) to increase the moveSpeed while its being held down and not after it's clicked (as shown in my script). ,I added a button to my canvas and i want to increase the moveSpeed WHILE the button is held down ...HOWWWW?!

using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine;

public class CarMovement : MonoBehaviour { public Rigidbody2D myrb2d; public float moveSpeed;

 public GameObject steeringWheel;
 
 
 // Use this for initialization
 void Start () {
     myrb2d.freezeRotation = true;
     myrb2d = GetComponent<Rigidbody2D>();
    
     
     
      }
 
 
 
 // Update is called once per frame
 void Update () {
     
     myrb2d.velocity = new Vector2(myrb2d.rotation/-65, moveSpeed);
     myrb2d.transform.rotation = steeringWheel.transform.rotation;

    
     }
 public void addSpeed()
 {
     
     moveSpeed = moveSpeed+ 1;
     

 }
   
 

}

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
0
Best Answer

Answer by osheaprogramming · Feb 02, 2019 at 11:54 PM

Hey @karim_fawaz,

This simple script utilising the IPointerDownHandler and IPointerUpHandler interfaces should help you in achieving what you're after. Simply replace the Button component on your UI Button with this then fill out the reference to CarMovement. The next thing I would look into implementing is applying a speed range for your car and some functionality to slow it back down while the button isn't being pressed.

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class GasButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

     //Public CarMovement  component
     public CarMovement carMovement;

     //Private boolean that will be true when button is pressed
     private bool isDown;
 
     public void OnPointerDown(PointerEventData eventData) {
 
         //Set isDown boolean
         this.isDown = true;
 
         //Enter functionality to be applied when button is pressed here
         Debug.Log("Button Pressed");
     }
 
     public void OnPointerUp(PointerEventData eventData) {
 
         //Set isDown boolean
         this.isDown = false;
 
         //Enter functionality to be applied when button is released here
         Debug.Log("Button Released");
     }
 
     void Update() {
 
         if (this.isDown) {
 
             //Enter functionality to be applied when button is held down here
             Debug.Log("Button Is Down");

             carMovement.addSpeed();
 
         }
     }
 }


Goodluck

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
avatar image
0

Answer by karim_fawaz · Feb 03, 2019 at 10:57 AM

thank you very much @osheaprogramming but can you give me more details about replacing the button? should i keep it? delete it?

Comment
Add comment · Show 5 · 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 osheaprogramming · Feb 03, 2019 at 01:14 PM 0
Share

No worries @karim_fawaz,

You should keep the 'Button' GameObject found in your hierarchy under your Canvas, all you need to do is replace the default Button Component found on your Button with the GasPedal script above.

If done correctly, as you press, hold and release the button you should see log's in your console showing you exactly what's happening. Once you achieve this you can make it do whatever you want in your Car$$anonymous$$ovement script.

Let me know how it goes

avatar image karim_fawaz osheaprogramming · Feb 03, 2019 at 02:23 PM 0
Share

Really appreciate it m8 thanks again @osheaprogram$$anonymous$$g !!

avatar image karim_fawaz osheaprogramming · Feb 03, 2019 at 09:56 PM 0
Share

It finally worked out @osheaprogram$$anonymous$$g thank you very much! I was wondering if you could also help me figure out how to make the moveSpeed decrease when the user isn't holding the gas pedal. I tried to make a function and implementing it to the OnPointerUp but it didn't work, I also tried to make a moveSpeed reference on the GasButton script and it also didn't work :( I just want this to happen after releasing the button: public void stopGas() { moveSpeed = moveSpeed - 0.4f; } Thanks again for the help m8 :)

avatar image osheaprogramming · Feb 04, 2019 at 09:30 AM 0
Share

I'm glad to hear it worked @karim_fawaz,

As for your new problems, they should be relatively easy to fix. Your issue is that you need to adding and subtracting from your speed variable on your car every frame. Calling it when the button is released means you will only subtract the amount from the speed once, you want to be subtracting every frame until the cars speed once again reaches 0.

The easiest way to implement this is to add a speed variable to your car which I'm sure you have, however I would suggest prefixing it with the Range attribute. It should look like this:

 [Range (0, 100)] public float speed;

Then in your GasButton script you should be adding or subtracting from the speed amount every frame depending on whether it's pressed or not. your Update method should then look something like this to achieve what you're after:

  void Update() {
  
      if (this.isDown) {
  
          //Enter functionality to be applied when button is held down here
          Debug.Log("Button Is Down");
          car$$anonymous$$ovement.addSpeed();
  
      }else{
         
         car$$anonymous$$ovement.decreaseSpeed();
      }
  }

Hopefully this helps, cheers

avatar image karim_fawaz osheaprogramming · Feb 04, 2019 at 02:32 PM 0
Share

thank you very much bro, you were very helpful! Hope I can help you in any shape or form in the near future @osheaprogram$$anonymous$$g

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

227 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Does anyone know of a way to assign an Axis or KeyCode to a GUI Button or Texture? I'm trying to make a 2D platformer on a mobile device. As you can tell, I have no Idea what I'm doing... 1 Answer

Playmaker editor won't show Please Help 1 Answer

How to Convert each 2D Array to GameObject 2 Answers

Input trick question 3 Answers

character controller sinks into platform while using raycast to detect collision 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