Create and use a Virtual Joystick in a UI for bevy Game Engine.
Aviable and compatible versions
| bevy | VirtualJoystick | |--------|-----------------| | 0.10.1 | 0.1.0 |
| Both | Horizontal | Vertical |
|--------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|
| |
|
|
| Fixed | Floating | Dynamic (TODO: Fix movement feel) |
|--------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| |
|
|
Check out the examples for details.
Add to Cargo.toml
toml
[dependencies]
bevy = "0.10.1"
virtual_joystick = "*" # Add your version
The minimal requirement: ```rs use bevy::prelude::; // import crate use virtual_joystick::;
fn main() { App::new() .addplugins(DefaultPlugins) // Add plugin to application .addplugin(VirtualJoystickPlugin) .run() } ```
Create Joystick ```rs
fn main() { App::new() .addplugins(DefaultPlugins) // Add plugin to application .addplugin(VirtualJoystickPlugin) // Create system .addstartupsystem(createscene) // update System .addsystem(update_player) .run() }
fn createscene(mut cmd: Commands, assetserver: Res
// Spawn Virtual Joystick at horizontal center
cmd.spawn(
// Define variable for Joystick
VirtualJoystickBundle::new(VirtualJoystickNode {
border_image: asset_server.load("Outline.png"),
knob_image: asset_server.load("Knob.png"),
knob_size: Vec2::new(80., 80.),
dead_zone: 0.,
})
.set_color(TintColor(Color::WHITE))
.set_style(Style {
size: Size::all(Val::Px(150.)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Percent(50.),
bottom: Val::Percent(15.),
..default()
},
..default()
}),
)
// When you add this component you mark this area as interactable for Joystick
.insert(VirtualJoystickInteractionArea);
} ```
Use variable generated by Joystick ```rs
fn updatejoystick(
mut joystick: EventReader
// Iter each joystick event
for j in joystick.iter() {
// get axis value 0-1 in x & y
let Vec2 { x, y } = j.axis();
// Move player using joystick axis value
player.translation.x += x * player_data.0 * time_step.period.as_secs_f32();
player.translation.y += y * player_data.0 * time_step.period.as_secs_f32();
}
} ```
```rs // Resource enum VirtualJoystickAxis { Both, // Default Horizontal, Vertical, }
// Resource enum VirtualJoystickType { /// Static position Fixed, /// Spawn at point click /// Default Floating, /// Follow point on drag Dynamic, }
// Component
struct VirtualJoystickNode {
/// Image for background or border image on joystick
pub borderimage: Handle
// EventReader struct VirtualJoystickEvent { /// Raw position of point (Mouse or Touch) pub fn value(&self) -> Vec2;
/// Axis of Joystick see [crate::VirtualJoystickAxis]
pub fn direction(&self) -> VirtualJoystickAxis;
/// Delta value ranging from 0 to 1 in each vector (x and y)
pub fn axis(&self) -> Vec2;
/// Delta value snaped
/// warn: Still working, not working properly
pub fn snap_value(&self) -> Vec2;
}
// Bundle to spawn struct VirtualJoystickBundle { pub fn new(joystick: VirtualJoystickNode) -> Self;
pub fn set_node(mut self, node: Node) -> Self;
pub fn set_style(mut self, style: Style) -> Self;
pub fn set_color(mut self, color: TintColor) -> Self;
pub fn set_focus_policy(mut self, focus_policy: FocusPolicy) -> Self;
pub fn set_transform(mut self, transform: Transform) -> Self;
pub fn set_global_transform(mut self, global_transform: GlobalTransform) -> Self;
pub fn set_visibility(mut self, visibility: Visibility) -> Self;
pub fn set_computed_visibility(mut self, computed_visibility: ComputedVisibility) -> Self;
pub fn set_z_index(mut self, z_index: ZIndex) -> Self;
} ```