Game Development Reference
In-Depth Information
private float leanValue;
private float maxLean = FastMath.QUARTER_PI * 0.5f;
5. In the onAction method, we make sure that we handle the corresponding input.
Again, after setting the Booleans like this, make sure our actions stay on until the
key is released:
if (binding.equals("LeanLeft")){
leanLeft = value;
} else if (binding.equals("LeanRight")){
leanRight = value;
} else if (binding.equals("LeanFree")){
leanFree = value;
}
6. Applying the leaning value is pretty straightforward. We do this in a method
called lean , which takes a float value as the input. First, we clamp leanValue
to make sure we don't exceed our maxLean value. Then, we set the rotation
along the z axis to the negative value as follows:
private void lean(float value){
FastMath.clamp(value, -maxLean, maxLean);
centerPoint.setLocalRotation(new
Quaternion().fromAngles(0, 0, -value));
}
7. One bit left now, and that's where to call this method from. In the update meth-
od, we add two blocks of code. This reads as: if the button for leaning left is
pressed and the leaning value is less than the maximum leaning value, lean more.
Otherwise, if the button for free leaning is not pressed and the lean value is more
than 0, lean less:
if(leanLeft && leanValue < maxLean){
lean(leanValue+= 0.5f * tpf);
} else if(!leanFree && leanValue > 0f){
lean(leanValue-= 0.5f * tpf);
}
8. This code block then needs to be mirrored to lean in the other direction.
9. That's it for controlling leaning with buttons only. To add leaning using the mouse
when leanFree is pressed, the onAnalog method needs a bit of work as well.
Search WWH ::




Custom Search