Wednesday, October 28, 2015

Unreal Engine 4 Quick Trick: Wheeled Vehicle Air Control


In R0V3R, I was looking to create mid-air vehicle control similar to Grand Theft Auto V. This means having the ability to control the pitch and roll of the vehicle when vehicle is no longer grounded.

In Unreal Engine 4, we can accomplish just that with a few simple steps!

The complete AirControl function created within Rover's player blueprint (Wheeled Vehicle).

First off, I created an new input event bound to my controller's left joystick Y+/- value and used my existing X+/- value (used for turning) as the driving force of the AirControl function.

 
Using the Axis Value to control Pitch and Rotation inputs.



From there the first thing we need to check is to see if the Actor is grounded or not. The easiest way was to detect if a raycast with a specified length at the root of the actor was penetrating an object along the -Z axis. To accomplish this, we gathered the Actor location via GetActorLocation. The start point of the ray is at the location, but in order to get the end point we needed to offset the Z value. By using Break Vector we are able to use a subtraction node with the desired length of the ray to be cast from the Z value of the actor location. By passing X and Y across and creating a new Z value, we can rebuild the new vector using Make Vector and plug it into the End input. Be sure to enable Ignore Self. 

The isGrounded sequence. Be sure to enable Ignore Self. Outputs a Boolean.
The hardest part is done. Now we need to set up how we are going to parse the raw values from our controller input into appropriate angular velocity to add to the Vehicle Movement>Updated Primitive.

Using the Relative Transform of the Vehicle Movement component we can create a new Direction Vector.
The last step is the most important. We use the Get Relative Transform node to return a transform value of the Vehicle Movement>Updated Primitive. Passing the return value to Transform Direction node, we utilize our raw controller input with a multiplier dictate the new angular velocity. We use the multipliers to adjust speed and if inputs are to be inverted by using a +/- value. This then gets plugged into Set Physics Angular Velocity > New Ang Vel Input which creates the additional force to control the actor in mid air. Be sure to set Add to Current as True because we want to add to the existing value. 

We use the Boolean return value from LineTraceByChannel to control weather we enable the ability to move the actor in mid air. By this we know that IF the actor is NOT grounded we can then Set Physics Angular Velocity.

IF actor is NOT grounded (false) we can use Air Control to manipulate actor mid-air.

In short we accomplished the following:
  • Detecting if Actor is grounded or not.
  • Created a new Angular Velocity with Raw Input and Transformed Direction into a new Vector.
  • Set an Additive Angular Velocity to control the actor while in mid-air.

No comments:

Post a Comment