- Home /
Car acceleration using Xbox 360 triggers?
After looking at these links
http://wiki.unity3d.com/index.php?title=Xbox360Controller http://docs.unity3d.com/Documentation/ScriptReference/KeyCode.html
I noticed there is a direct way to enable those triggers on a 360 controller, or anything of similar nature. They are Six Axis or something for Right Trigger, and Fifth Axis for Left Trigger...on the Mac. Regardless, on Windows, they mix up the "Nth Axis" inputs, so Windows and Mac are different.
But I just need to know how do you call these here because it tells me error CS0029: Cannot implicitly convert type `float' to bool' ? Lines highlighted down below in bold.
Let's say it was this script:
bool accelKey = Input.GetKey (KeyCode.UpArrow);
float accelKey2 = Input.GetAxis ("6th axis (Joysticks)");
bool brakeKey = Input.GetKey (KeyCode.DownArrow);
float brakeKey2 = Input.GetAxis ("5th axis (Joysticks)");
if (drivetrain.automatic && drivetrain.gear == 0)
{
accelKey = Input.GetKey (KeyCode.DownArrow);
accelKey2 = Input.GetAxis("5th axis (Joysticks)");
brakeKey = Input.GetKey (KeyCode.UpArrow);
brakeKey2 = Input.GetAxis("6th axis (Joysticks)");
}
if (Input.GetKey (KeyCode.LeftShift))
{
throttle += Time.deltaTime / throttleTime;
throttleInput += Time.deltaTime / throttleTime;
}
else if (accelKey)
{
if (drivetrain.slipRatio < 0.10f)
throttle += Time.deltaTime / throttleTime;
else if (!tractionControl)
throttle += Time.deltaTime / throttleTimeTraction;
else
throttle -= Time.deltaTime / throttleReleaseTime;
if (throttleInput < 0)
throttleInput = 0;
throttleInput += Time.deltaTime / throttleTime;
brake = 0;
}
**else if (accelKey2)**
{
if (drivetrain.slipRatio < 0.10f)
throttle += Time.deltaTime / throttleTime;
else if (!tractionControl)
throttle += Time.deltaTime / throttleTimeTraction;
else
throttle -= Time.deltaTime / throttleReleaseTime;
if (throttleInput < 0)
throttleInput = 0;
throttleInput += Time.deltaTime / throttleTime;
brake = 0;
}
else
{
if (drivetrain.slipRatio < 0.2f)
throttle -= Time.deltaTime / throttleReleaseTime;
else
throttle -= Time.deltaTime / throttleReleaseTimeTraction;
}
throttle = Mathf.Clamp01 (throttle);
if (brakeKey)
{
if (drivetrain.slipRatio < 0.2f)
brake += Time.deltaTime / throttleTime;
else
brake += Time.deltaTime / throttleTimeTraction;
throttle = 0;
throttleInput -= Time.deltaTime / throttleTime;
}
**if (brakeKey2)**
{
if (drivetrain.slipRatio < 0.2f)
brake += Time.deltaTime / throttleTime;
else
brake += Time.deltaTime / throttleTimeTraction;
throttle = 0;
throttleInput -= Time.deltaTime / throttleTime;
}
else
{
if (drivetrain.slipRatio < 0.2f)
brake -= Time.deltaTime / throttleReleaseTime;
else
brake -= Time.deltaTime / throttleReleaseTimeTraction;
}
brake = Mathf.Clamp01 (brake);
throttleInput = Mathf.Clamp (throttleInput, -1, 1);
// Handbrake
handbrake = Mathf.Clamp01 ( handbrake + (Input.GetKey (KeyCode.Space)? Time.deltaTime: -Time.deltaTime) );
// Gear shifting
float shiftThrottleFactor = Mathf.Clamp01((Time.time - lastShiftTime)/shiftSpeed);
drivetrain.throttle = throttle * shiftThrottleFactor;
drivetrain.throttleInput = throttleInput;
if(Input.GetKeyDown(KeyCode.R))
{
lastShiftTime = Time.time;
drivetrain.ShiftUp ();
}
if(Input.GetKeyDown(KeyCode.F))
{
lastShiftTime = Time.time;
drivetrain.ShiftDown ();
}
// Apply inputs
foreach(Wheel w in wheels)
{
w.brake = brake;
w.handbrake = handbrake;
w.steering = steering;
}
}
$$anonymous$$ind posting what line the error is on?
Check the stars man. Sorry they weren't formatted to bold properly.
@$$anonymous$$etwuns The standard way of presenting the lines are by just giving the line number. Formatting them in some way makes them harder to find and easier to miss.
Also note that in a code section you can't change formatting for obvious reasons :)
If I answered your question, please accept my answer :)
Answer by Benproductions1 · Aug 21, 2013 at 01:58 AM
Hello,
If we take a look at if
statements, we can always express:
if (condition)
to be the same thing as
if (condition == True)
Going by this example, lets take a look at your code:
Looking at the lines with the error, we can see that you are checking:
if (brakeKey2)
Which is (according to our previous definition) the same as
if (brakeKey2 == True)
If we read the error, it looks like you are trying to convert a float
type object to a bool
type object (which you can't do).
So we take a look at the definition for brakeKey2
(as it's the only variable on that line):
float brakeKey2 = Input.GetAxis(/*etc*/)
It is, as expected, a floating point variable.
The simple fact remains, is that you are trying to do something like 8.65 == True
. How do you compare a float
and a bool
?
You can't.
What you need to do is write an expression that evaluates into a bool.
For instance:
brakeKey2 == 0.0f
or
brakeKey2 < 0.1 && brakeKey2 > -0.1
of maybe?
!float.isNull(brakeKey2)
Now your if
statement is getting a boolean value and not a float.
Hope this helps, :)
Benproductions1