Question by
BeamCRASH · May 18, 2021 at 09:11 AM ·
c#scripting beginner
Detect if UI Button is pressed
Hello, So I have a script taht whenever I press the Space key I can drive a car. This works fine, but I'd like to have it so that only when you press a GUI Button the car drives. Here's the current script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CarControl : MonoBehaviour {
WheelJoint2D wj;
JointMotor2D mo;
void Start() {
wj = gameObject.GetComponents<WheelJoint2D>()[0];
mo = new JointMotor2D();
}
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
mo.motorSpeed = -1000;
mo.maxMotorTorque = 1000;
wj.useMotor=true;
wj.motor = mo;
}
if (Input.GetKeyUp(KeyCode.Space)) {
wj.useMotor = false;
}
}
}
Thanks!
Comment