Question by
Hathakas · Dec 28, 2016 at 09:44 PM ·
button trigger eventskeycode
Is there anyway to force Keycode.w through UI button?
I'm making a car game, the car moved when W is pressed, and breaks/reverses with S. I need to make it mobile friendly so I've added pedals UI buttons. Here are my attempts.
static void MyKeyDown()
{
Input.GetKeyDown("w");
}
public void GasPedalDown()
{
gasIsPressed = true;
pedalImage.sprite = pressedDown;
}
void Update () {
if (gasIsPressed)
{
MyKeyDown();
Debug.Log("I AM CONTINUOUS");
}
}
GasPedalDown() is called from the event on the button pointer down, gasIsPressed becomes false once I let go of the button. This is one thing I tried. I also have the carController run the same functions while gasIsPressed = true, as it would if keycode.w was down but it's not working.
if (gear_1 && Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.S) | gP.brakeIsPressed | gP.gasIsPressed && virtualAxis < Gear_1_MaxSpeed)
{
//JointMotor2D tempMotor = BackWheelJoint2D.motor;
// tempMotor.maxMotorTorque = Gear_1_Torque;
virtualAxisFront += Gear_1_Acceleration;
virtualAxis += Gear_1_Acceleration;
}
if (Input.GetKey(KeyCode.W) | gP.gasIsPressed) // only run motor when w is pressed down
{
if (rb.velocity.x >= 0) // check to see if the car (not the wheel) is movein, so we can BRAKE
{
myWheelJoin2D[0].useMotor = false;
BackMotor.motorSpeed = -1 * Mathf.Abs(virtualAxis) * carSpeedMultiplier;
myWheelJoin2D[1].motor = BackMotor;
if (is4x4)
{
FrontMotor.motorSpeed = -1 * Mathf.Abs(virtualAxisFront) * carSpeedMultiplier;
myWheelJoin2D[0].motor = FrontMotor;
}
if (is8x8)
{
FrontMotor.motorSpeed = -1 * Mathf.Abs(virtualAxisFront) * carSpeedMultiplier;
myWheelJoin2D[0].motor = FrontMotor;
FrontMiddleMotor.motorSpeed = -1 * Mathf.Abs(virtualAxisFront) * carSpeedMultiplier;
myWheelJoin2D[2].motor = FrontMiddleMotor;
BackMiddleMotor.motorSpeed = -1 * Mathf.Abs(virtualAxis) * carSpeedMultiplier;
myWheelJoin2D[3].motor = BackMiddleMotor;
}
}
Thanks!
Comment