- Home /
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;
}
}
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
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?
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
Really appreciate it m8 thanks again @osheaprogram$$anonymous$$g !!
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 :)
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
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