use bevy::prelude::*;
use bevy::input::mouse::{MouseMotion, MouseWheel};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup_camera)
.add_system(camera_movement)
.run();
}
#[derive(Resource)]
struct CameraState {
pitch: f32, // 上下俯仰
yaw: f32, // 左右偏航
}
fn setup_camera(mut commands: Commands) {
// 初始相機位置和面向
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 1.0, 5.0)
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
CameraState { pitch: 0.0, yaw: 0.0 },
));
}
fn camera_movement(
time: Res<Time>,
keys: Res<Input<KeyCode>>,
mut mouse_motion_events: EventReader<MouseMotion>,
mut query: Query<(&mut Transform, &mut CameraState), With<Camera>>,
) {
let (mut transform, mut state) = query.single_mut();
// 相機旋轉參數,調整靈敏度
let mouse_sensitivity = 0.005;
// 接收滑鼠移動事件,更新 yaw & pitch
for event in mouse_motion_events.iter() {
state.yaw -= event.delta.x * mouse_sensitivity;
state.pitch -= event.delta.y * mouse_sensitivity;
// 限制俯仰範圍,避免翻轉
state.pitch = state.pitch.clamp(-std::f32::consts::FRAC_PI_2, std::f32::consts::FRAC_PI_2);
}
// 利用 yaw 與 pitch 建立旋轉四元數
let yaw_rotation = Quat::from_rotation_y(state.yaw);
let pitch_rotation = Quat::from_rotation_x(state.pitch);
transform.rotation = yaw_rotation * pitch_rotation;
// 相機移動參數
let move_speed = 5.0;
// 前向與右向向量 (考慮水平向量)
let forward = transform.forward();
let right = transform.right();
// 判斷按鍵改變位移向量
let mut direction = Vec3::ZERO;
if keys.pressed(KeyCode::W) {
direction += forward;
}
if keys.pressed(KeyCode::S) {
direction -= forward;
}
if keys.pressed(KeyCode::A) {
direction -= right;
}
if keys.pressed(KeyCode::D) {
direction += right;
}
// 按空白提升高度,Ctrl 下降
if keys.pressed(KeyCode::Space) {
direction += Vec3::Y;
}
if keys.pressed(KeyCode::LControl) {
direction -= Vec3::Y;
}
if direction.length_squared() > 0.0 {
direction = direction.normalize();
transform.translation += direction * move_speed * time.delta_seconds();
}
}
User
warning: unused import: MouseWheel
--> src\main.rs:2:39
|
2 | use bevy::input::mouse::{MouseMotion, MouseWheel};
| ^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
error[E0277]: the trait bound CameraState: bevy::prelude::Component is not satisfied
--> src\main.rs:34:16
|
34 | mut query: Query<(&mut Transform, &mut CameraState), With>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait bevy::prelude::Component is not implemented for CameraState
|
= help: the following other types implement trait bevy::prelude::Component:
Aabb
AabbGizmo
AccessibilityNode
Anchor
AnimationPlayer
AudioSink
BackgroundColor
BloomSettings
and 132 others
= note: required for &mut CameraState to implement WorldQuery
= note: 1 redundant requirement hidden
= note: required for (&mut bevy::prelude::Transform, &mut CameraState) to implement WorldQuery
note: required by a bound in bevy::prelude::Query
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\system\query.rs:325:37
|
325 | pub struct Query<'world, 'state, Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
| ^^^^^^^^^^ required by this bound in Query
warning: use of deprecated method bevy::prelude::App::add_startup_system: Please use add_systems instead. If you didn't change the default base set, you should use add_systems(Startup, your_system).
--> src\main.rs:7:10
|
7 | .add_startup_system(setup_camera)
| ^^^^^^^^^^^^^^^^^^
|
= note: #[warn(deprecated)] on by default
warning: use of deprecated method bevy::prelude::App::add_system: Please use add_systems instead. If you didn't change the default base set, you should use add_systems(Update, your_system).
--> src\main.rs:8:10
|
8 | .add_system(camera_movement)
| ^^^^^^^^^^
error[E0277]: the trait bound for<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> fn(bevy::prelude::Res<'a, bevy::prelude::Time>, bevy::prelude::Res<'b, bevy::prelude::Input<bevy::prelude::KeyCode>>, bevy::prelude::EventReader<'c, 'd, MouseMotion>, bevy::prelude::Query<'e, 'f, (&'g mut bevy::prelude::Transform, &'h mut CameraState), bevy::prelude::With<bevy::prelude::Camera>>) {camera_movement}: IntoSystemConfigs<_> is not satisfied
--> src\main.rs:8:21
|
8 | .add_system(camera_movement)
| ---------- ^^^^^^^^^^^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
= help: the trait IntoSystem<(), (), _> is not implemented for fn item
= help: the following other types implement trait :
implements
implements
implements
implements
implements
implements
implements
implements
and 14 others
= note: required for to implement
note: required by a bound in
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.11.3\src\app.rs:380:50 |
380 | pub fn add_system(&mut self, system: impl IntoSystemConfigs) -> &mut Self {
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in
= note: the full name for the type has been written to 'C:\Users\fer52\mynewcargo\my_project\target\debug\deps\my_project.long-type-6961791715131101330.txt'
= note: consider using to print the full type name to the console
Assistant
你這裡遇到的問題主要有兩方面:
Bevy 0.11 API 已改變,add_startup_system/add_system 要改用 .add_systems()
Compiling my_project v0.1.0 (C:\Users\fer52\mynewcargo\my_project)
error[E0599]: no variant or associated item named LControlLeft found for enum bevy::prelude::KeyCode in
the current scope
--> src\main.rs:72:30
|
72 | if keys.pressed(KeyCode::LControlLeft) {
| ^^^^^^^^^^^^ variant or associated item not found in KeyCode
|
help: there is a variant with a similar name
|
72 | if keys.pressed(KeyCode::ControlLeft) {
| ~~~~~~~~~~~
For more information about this error, try rustc --explain E0599.
let mouse_sensitivity = 0.005;
for event in mouse_motion_events.iter() {
state.yaw -= event.delta.x * mouse_sensitivity;
state.pitch -= event.delta.y * mouse_sensitivity;
state.pitch = state.pitch.clamp(-std::f32::consts::FRAC_PI_2, std::f32::consts::FRAC_PI_2);
}
let yaw_rotation = Quat::from_rotation_y(state.yaw);
let pitch_rotation = Quat::from_rotation_x(state.pitch);
transform.rotation = yaw_rotation * pitch_rotation;
let move_speed = 5.0;
let forward = transform.forward().normalize();
let right = transform.right().normalize();
let mut direction = Vec3::ZERO;
if keys.pressed(KeyCode::W) {
direction += forward;
}
if keys.pressed(KeyCode::S) {
direction -= forward;
}
if keys.pressed(KeyCode::A) {
direction -= right;
}
if keys.pressed(KeyCode::D) {
direction += right;
}
// Bevy KeyCode 沒有 LControl,改用 LControlLeft(或按鍵盤實際對應)
if keys.pressed(KeyCode::Space) {
direction += Vec3::Y;
}
if keys.pressed(KeyCode::ControlLeft) {
direction -= Vec3::Y;
}
if direction.length_squared() > 0.0 {
direction = direction.normalize();
transform.translation += direction * move_speed * time.delta_seconds();
}
}
可以加入一個地面嗎
Assistant
當然可以!要在你的 Bevy 3D 相機範例中加入一個「地面」,通常做法是在場景裡放一個平面 (plane) 實體,讓相機可以看到它並且作為視覺參考。
error[E0433]: failed to resolve: use of undeclared crate or module bevy_rapier3d
--> src\main.rs:5:5
|
5 | use bevy_rapier3d::prelude::*;
| ^^^^^^^^^^^^^ use of undeclared crate or module bevy_rapier3d
error[E0412]: cannot find type NoUserData in this scope
--> src\main.rs:11:44
|
11 | .add_plugins(RapierPhysicsPlugin::::default())
| ^^^^^^^^^^ not found in this scope
error[E0412]: cannot find type Velocity in this scope
--> src\main.rs:80:46
|
80 | mut query: Query<(&mut CameraState, &mut Velocity, &Transform), With>,
| ^^^^^^^^ not found in this scope
|
help: you might be missing a type parameter
|
76 | fn camera_movement(
| ++++++++++
error[E0425]: cannot find value commands in this scope
--> src\main.rs:106:5
|
106 | commands.entity(query.single().id()).insert(Transform {
| ^^^^^^^^ not found in this scope
error[E0433]: failed to resolve: use of undeclared type RapierPhysicsPlugin
--> src\main.rs:11:22
|
11 | .add_plugins(RapierPhysicsPlugin::::default())
| ^^^^^^^^^^^^^^^^^^^ use of undeclared type RapierPhysicsPlugin
error[E0277]: the trait bound for<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i> fn(bevy::prelude::Res<'a, bevy::prelude::Time>, bevy::prelude::Res<'b, bevy::prelude::Input<bevy::prelude::KeyCode>>, bevy::prelude::EventReader<'c, 'd, MouseMotion>, bevy::prelude::Query<'e, 'f, (&'g mut CameraState, &'h mut {type error}, &'i bevy::prelude::Transform), bevy::prelude::With<Player>>) {camera_movement}: IntoSystemConfigs<_> is not satisfied
--> src\main.rs:14:30
|
14 | .add_systems(Update, camera_movement)
| ----------- ^^^^^^^^^^^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
= help: the trait IntoSystem<(), (), _> is not implemented for fn item fn(Res<'a, Time>, ..., ..., ...) {camera_movement}
= help: the following other types implement trait IntoSystemConfigs<Marker>:
(S0, S1) implements IntoSystemConfigs<(SystemConfigTupleMarker, P0, P1)>(S0, S1, S2) implements IntoSystemConfigs<(SystemConfigTupleMarker, P0, P1, P2)>(S0, S1, S2, S3) implements IntoSystemConfigs<(SystemConfigTupleMarker, P0, P1, P2, P3)> (S0, S1, S2, S3, S4) implements IntoSystemConfigs<(SystemConfigTupleMarker, P0, P1, P2, P3, P4)>(S0, S1, S2, S3, S4, S5) implements IntoSystemConfigs<(SystemConfigTupleMarker, P0, P1, P2, P3, P4, P5)>
implements
implements
implements
and 14 others
= note: required for to implement
note: required by a bound in
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.11.3\src\app.rs:404:23 |
401 | pub fn add_systems(
| ----------- required by a bound in this associated function
...
404 | systems: impl IntoSystemConfigs,
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in
= note: the full name for the type has been written to 'C:\Users\fer52\mynewcargo\my_project\target\debug\deps\my_project.long-type-9105446942307829051.txt'
= note: consider using to print the full type name to the console
features = ["render"] 是選擇性功能,啟用會在視窗中繪製物理 debug 線框,方便調試。
2. 在程式中引入 bevy_rapier3d::prelude::*
rust
use bevy_rapier3d::prelude::*;
3. 確認 cargo build 或 cargo run 後能正常編譯
完整示範:
rust
User
error[E0425]: cannot find value commands in this scope
--> src\main.rs:106:5
|
106 | commands.entity(query.single().id()).insert(Transform {
| ^^^^^^^^ not found in this scope
error[E0277]: the trait bound bevy_rapier3d::dynamics::Velocity: bevy::prelude::Component is not satisfied because the trait comes from a different crate version
--> src\main.rs:80:16
|
80 | mut query: Query<(&mut CameraState, &mut Velocity, &Transform), With>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait bevy::prelude::Component is not implemented for bevy_rapier3d::dynamics::Velocity
|
note: there are multiple different versions of crate bevy_ecs in the dependency graph
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\component.rs:149:1
|
149 | pub trait Component: Send + Sync + 'static {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the required trait
|
::: src\main.rs:1:5
|
1 | use bevy::prelude::;
| ---- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
...
5 | use bevy_rapier3d::prelude::;
| ------------- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_rapier3d-0.29.0\src\dynamics\rigid_body.rs:88:1
|
88 | pub struct Velocity {
| ------------------- this type doesn't implement the required trait
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.15.3\src\archetype.rs:154:1
|
154 | pub(crate) trait BundleComponentStatus {
| -------------------------------------- this is the found trait
= help: you can use cargo tree to explore your dependency tree
error[E0277]: the trait bound bevy_rapier3d::plugin::RapierPhysicsPlugin: Plugins<_> is not satisfied because the trait comes from a different crate version
--> src\main.rs:11:22
|
11 | .add_plugins(RapierPhysicsPlugin::::default())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
|
= help: the trait bevy_app::plugin::sealed::Plugins<_> is not implemented for bevy_rapier3d::plugin::RapierPhysicsPlugin
help: trait impls with same name found
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.15.3\src\plugin.rs:144:5
|
144 | impl<P: Plugin> Plugins for P {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
157 | impl<P: PluginGroup> Plugins for P {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: perhaps two different versions of crate bevy_app are being used?
note: there are multiple different versions of crate bevy_app in the dependency graph
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.11.3\src\plugin.rs:83:5
|
83 | pub trait Plugins {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ this is the required trait
|
::: src\main.rs:1:5
|
1 | use bevy::prelude::;
| ------------- one version of crate is used here, as a dependency of crate
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_rapier3d-0.29.0\src\plugin\plugin.rs:25:1
|
25 | pub struct RapierPhysicsPlugin<PhysicsHooks = ()> {
| ------------------------------------------------- this type doesn't implement the required trait
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_app-0.15.3\src\app.rs:24:1
|
24 | / bevy_ecs::define_label!(
25 | | /// A strongly-typed class of labels used to identify an [].
26 | | AppLabel,
27 | | APP_LABEL_INTERNER
28 | | );
| |_- this is the found trait
= help: you can use to explore your dependency tree
error[E0277]: the trait bound (bevy::prelude::Camera3dBundle, CameraState): Bundle is not satisfied
--> src\main.rs:20:20
|
20 | commands.spawn((
| _________-----_^
| | |
| | required by a bound introduced by this call
21 | | Camera3dBundle {
22 | | transform: Transform::from_xyz(0.0, 1.0, 5.0)
23 | | .looking_at(Vec3::ZERO, Vec3::Y),
... |
26 | | CameraState { pitch: 0.0, yaw: 0.0 },
27 | | ));
| |^ the trait Bundle is not implemented for (bevy::prelude::Camera3dBundle, CameraState)
|
= help: the following other types implement trait Bundle:
()
(B0, B1)
(B0, B1, B2)
(B0, B1, B2, B3)
(B0, B1, B2, B3, B4)
(B0, B1, B2, B3, B4, B5)
(B0, B1, B2, B3, B4, B5, B6)
(B0, B1, B2, B3, B4, B5, B6, B7)
and 8 others
note: required by a bound in bevy::prelude::Commands::<'w, 's>::spawn
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\system\commands\mod.rs:267:25
|
267 | pub fn spawn<'a, T: Bundle>(&'a mut self, bundle: T) -> EntityCommands<'w, 's, 'a> {
| ^^^^^^ required by this bound in Commands::<'w, 's>::spawn
error[E0599]: the method single_mut exists for struct Query<'_, '_, (&mut Transform, &mut ...), ...>, but its trait bounds were not satisfied
--> src\main.rs:36:44
|
13 | struct CameraState {
| ------------------ doesn't satisfy CameraState: bevy::prelude::Component
...
36 | let (mut transform, mut state) = query.single_mut();
| ^^^^^^^^^^ method cannot be called on Query<'_, '_, (&mut Transform, &mut ...), ...> due to unsatisfied trait bounds
|
= note: the full type name has been written to 'C:\Users\fer52\mynewcargo\my_project\target\debug\deps\my_project.long-type-677695322970901595.txt'
= note: consider using --verbose to print the full type name to the console
= note: the following trait bounds were not satisfied:
CameraState: bevy::prelude::Component
note: the trait bevy::prelude::Component must be implemented
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\component.rs:149:1
|
149 | pub trait Component: Send + Sync + 'static {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0599]: no variant or associated item named LControl found for enum bevy::prelude::KeyCode in the
current scope
--> src\main.rs:81:30
|
81 | if keys.pressed(KeyCode::LControl) {
| ^^^^^^^^ variant or associated item not found in KeyCode
Some errors have detailed explanations: E0277, E0599.
For more information about an error, try rustc --explain E0277.
use bevy::prelude::*;
use bevy::input::mouse::MouseMotion;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// Bevy 0.11 改成 add_systems API
.add_systems(Startup, setup_camera)
.add_systems(Update, camera_movement)
.run();
}
#[derive(Component)] // 加上這個,CameraState才是組件
struct CameraState {
pitch: f32,
yaw: f32,
}
fn setup_camera(mut commands: Commands) {
// 先 spawn Camera3dBundle,再插入 CameraState 組件
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 1.0, 5.0)
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraState { pitch: 0.0, yaw: 0.0 });
}
fn camera_movement(
time: Res<Time>,
keys: Res<Input<KeyCode>>,
mut mouse_motion_events: EventReader<MouseMotion>,
mut query: Query<(&mut Transform, &mut CameraState), With<Camera>>,
) {
let (mut transform, mut state) = query.single_mut();
let mouse_sensitivity = 0.005;
for event in mouse_motion_events.iter() {
state.yaw -= event.delta.x * mouse_sensitivity;
state.pitch -= event.delta.y * mouse_sensitivity;
state.pitch = state.pitch.clamp(-std::f32::consts::FRAC_PI_2, std::f32::consts::FRAC_PI_2);
}
let yaw_rotation = Quat::from_rotation_y(state.yaw);
let pitch_rotation = Quat::from_rotation_x(state.pitch);
transform.rotation = yaw_rotation * pitch_rotation;
let move_speed = 5.0;
let forward = transform.forward().normalize();
let right = transform.right().normalize();
let mut direction = Vec3::ZERO;
if keys.pressed(KeyCode::W) {
direction += forward;
}
if keys.pressed(KeyCode::S) {
direction -= forward;
}
if keys.pressed(KeyCode::A) {
direction -= right;
}
if keys.pressed(KeyCode::D) {
direction += right;
}
// Bevy KeyCode 沒有 LControl,改用 LControlLeft(或按鍵盤實際對應)
if keys.pressed(KeyCode::Space) {
direction += Vec3::Y;
}
if keys.pressed(KeyCode::LControlLeft) {
direction -= Vec3::Y;
}
if direction.length_squared() > 0.0 {
direction = direction.normalize();
transform.translation += direction * move_speed * time.delta_seconds();
}
}
error[E0433]: failed to resolve: use of undeclared type Collider
--> src\main.rs:45:9
|
45 | Collider::cuboid(10.0, 0.1, 10.0), // 碰撞盒大小與平面近似
| ^^^^^^^^ use of undeclared type Collider
error[E0433]: failed to resolve: use of undeclared type RigidBody
--> src\main.rs:46:9
|
46 | RigidBody::Fixed, // 靜態剛體,不會移動
| ^^^^^^^^^ use of undeclared type RigidBody
error[E0433]: failed to resolve: use of undeclared type RigidBody
--> src\main.rs:58:9
|
58 | RigidBody::Dynamic, // 動態剛體(會受物理影響)
| ^^^^^^^^^ use of undeclared type RigidBody
error[E0433]: failed to resolve: use of undeclared type Collider
--> src\main.rs:59:9
|
59 | Collider::capsule_y(0.9, 0.3), // 膠囊碰撞體,高約1.8米適合人型
| ^^^^^^^^ use of undeclared type Collider
error[E0433]: failed to resolve: use of undeclared type Velocity
--> src\main.rs:60:9
|
60 | Velocity::zero(), // 初始速度為 0
| ^^^^^^^^ use of undeclared type Velocity
error[E0433]: failed to resolve: use of undeclared type LockedAxes
--> src\main.rs:61:9
|
61 | LockedAxes::ROTATION_LOCKED, // 鎖定旋轉(不滾動)
| ^^^^^^^^^^ use of undeclared type LockedAxes
Some errors have detailed explanations: E0277, E0412, E0425, E0433.
For more information about an error, try rustc --explain E0277.
use bevy::prelude::*;
use bevy::input::mouse::MouseMotion;
use bevy_rapier3d::prelude::*; // 記得加這行
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(RapierPhysicsPlugin::<NoUserData>::default())
//.add_plugins(RapierDebugRenderPlugin::default()) // 可開啟物理偵錯線框
.add_systems(Startup, setup)
.add_systems(Update, camera_movement)
.run();
}
#[derive(Component)]
struct CameraState {
pitch: f32,
yaw: f32,
}
#[derive(Component)]
struct Player;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// 平台
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
size: 20.0,
subdivisions: 1,
})),
material: materials.add(StandardMaterial {
base_color: Color::DARK_GREEN,
..default()
}),
..default()
},
Collider::cuboid(10.0, 0.1, 10.0),
RigidBody::Fixed,
));
// 玩家(攝影機)
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 1.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
CameraState { pitch: 0.0, yaw: 0.0 },
Player,
RigidBody::Dynamic,
Collider::capsule_y(0.9, 0.3),
Velocity::zero(),
LockedAxes::ROTATION_LOCKED,
));
// 光源
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
}
fn camera_movement(
time: Res<Time>,
keys: Res<Input<KeyCode>>,
mut mouse_motion_events: EventReader<MouseMotion>,
mut query: Query<(&mut CameraState, &mut Velocity, &Transform), With<Player>>,
mut commands: Commands, // 這裡要加入 commands 參數,才能用 commands.entity(...)
) {
let (mut state, mut velocity, transform) = query.single_mut();
let mouse_sensitivity = 0.005;
for event in mouse_motion_events.iter() {
state.yaw -= event.delta.x * mouse_sensitivity;
state.pitch -= event.delta.y * mouse_sensitivity;
state.pitch = state.pitch.clamp(-std::f32::consts::FRAC_PI_2, std::f32::consts::FRAC_PI_2);
}
let yaw_rot = Quat::from_rotation_y(state.yaw);
let pitch_rot = Quat::from_rotation_x(state.pitch);
let rotation = yaw_rot * pitch_rot;
// 透過 commands 更新 Transform,讓相機跟著旋轉
commands.entity(query.single().id()).insert(Transform {
rotation,
..*transform
});
let move_speed = 5.0;
let forward = rotation * Vec3::Z;
let right = rotation * Vec3::X;
let mut direction = Vec3::ZERO;
if keys.pressed(KeyCode::W) {
direction += forward;
}
if keys.pressed(KeyCode::S) {
direction -= forward;
}
if keys.pressed(KeyCode::A) {
direction -= right;
}
if keys.pressed(KeyCode::D) {
direction += right;
}
if keys.pressed(KeyCode::Space) {
direction += Vec3::Y;
}
if keys.pressed(KeyCode::ControlLeft) {
direction -= Vec3::Y;
}
if direction.length_squared() > 0.0 {
velocity.linvel = direction.normalize() * move_speed;
} else {
velocity.linvel = Vec3::ZERO;
}
}
小結
沒加 bevy_rapier3d 依賴會找不到模組和型別。
Cargo.toml 內一定要加入 bevy_rapier3d。
你需要重新 cargo build,系統才會抓到這個 crate。
bevy_rapier3d 需要和 bevy 版本相容,盡量版本號搭配。
如果你需要,我可以幫你整理完整專案 Cargo.toml 範例以及指令,要隨時告訴我!
;
| ---- one version of crate bevy_app is used here, as a dependency of crate bevy_internal
...
5 | use bevy_rapier3d::prelude::
bevy_app
bevy_internal
App
cargo tree
error[E0277]: the trait bound for<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i> fn(bevy::prelude::Res<'a, bevy::prelude::Time>, bevy::prelude::Res<'b, bevy::prelude::Input<bevy::prelude::KeyCode>>, bevy::prelude::EventReader<'c, 'd, MouseMotion>, bevy::prelude::Query<'e, 'f, (&'g mut CameraState, &'h mut bevy_rapier3d::dynamics::Velocity, &'i bevy::prelude::Transform), bevy::prelude::With<Player>>) {camera_movement}: IntoSystemConfigs<_> is not satisfied because the trait comes from a different crate version
--> src\main.rs:14:30
|
14 | .add_systems(Update, camera_movement)
| ^^^^^^^^^^^^^^^ unsatisfied trait bound
|
= help: the trait IntoSystem<(), (), _> is not implemented for fn item fn(Res<'a, Time>, ..., ..., ...) {camera_movement}
note: there are multiple different versions of crate bevy_ecs in the dependency graph
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\system\mod.rs:145:1
|
145 | pub trait IntoSystem<In, Out, Marker>: Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the required trait
|
::: src\main.rs:1:5
|
1 | use bevy::prelude::;
| ---- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
...
5 | use bevy_rapier3d::prelude::;
| ------------- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.15.3\src\archetype.rs:154:1
|
154 | pub(crate) trait BundleComponentStatus {
| -------------------------------------- this is the found trait
= help: you can use cargo tree to explore your dependency tree
error[E0277]: the trait bound (MaterialMeshBundle<bevy::prelude::StandardMaterial>, bevy_rapier3d::geometry::Collider, bevy_rapier3d::dynamics::RigidBody): Bundle is not satisfied because the trait comes from a different crate version
--> src\main.rs:33:20
|
33 | commands.spawn((
| _______________^
34 | | PbrBundle {
35 | | mesh: meshes.add(Mesh::from(shape::Plane {
36 | | size: 20.0,
... |
46 | | RigidBody::Fixed, // 靜態剛體,不會移動
47 | | ));
| |^ the trait Bundle is not implemented for (MaterialMeshBundle<StandardMaterial>, Collider, ...)
|
note: there are multiple different versions of crate bevy_ecs in the dependency graph
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\bundle.rs:142:1
|
142 | pub unsafe trait Bundle: DynamicBundle + Send + Sync + 'static {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the required trait
|
::: src\main.rs:1:5
|
1 | use bevy::prelude::;
| ---- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
...
5 | use bevy_rapier3d::prelude::;
| ------------- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.15.3\src\archetype.rs:154:1
|
154 | pub(crate) trait BundleComponentStatus {
| -------------------------------------- this is the found trait
= help: you can use cargo tree to explore your dependency tree
error[E0277]: the trait bound (bevy::prelude::Camera3dBundle, CameraState, Player, bevy_rapier3d::dynamics::RigidBody, bevy_rapier3d::geometry::Collider, bevy_rapier3d::dynamics::Velocity, bevy_rapier3d::dynamics::LockedAxes): Bundle is not satisfied because the trait comes from a different crate version
--> src\main.rs:50:20
|
50 | commands.spawn((
| _______________^
51 | | Camera3dBundle {
52 | | transform: Transform::from_xyz(0.0, 1.0, 5.0)
53 | | .looking_at(Vec3::ZERO, Vec3::Y),
... |
61 | | LockedAxes::ROTATION_LOCKED, // 鎖定旋轉(不滾動)
62 | | ));
| |^ the trait Bundle is not implemented for (Camera3dBundle, ..., ..., ..., ..., ..., ...)
|
note: there are multiple different versions of crate bevy_ecs in the dependency graph
--> C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.11.3\src\bundle.rs:142:1
|
142 | pub unsafe trait Bundle: DynamicBundle + Send + Sync + 'static {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this is the required trait
|
::: src\main.rs:1:5
|
1 | use bevy::prelude::;
| ---- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
...
5 | use bevy_rapier3d::prelude::;
| ------------- one version of crate bevy_ecs is used here, as a dependency of crate bevy_app
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.15.3\src\archetype.rs:154:1
|
154 | pub(crate) trait BundleComponentStatus {
| -------------------------------------- this is the found trait
= help: you can use cargo tree to explore your dependency tree
error[E0599]: the method single_mut exists for struct Query<'_, '_, (&mut CameraState, &mut ..., ...), ...>, but its trait bounds were not satisfied
--> src\main.rs:82:54
|
82 | let (mut state, mut velocity, transform) = query.single_mut();
| ^^^^^^^^^^ method cannot be called due to unsatisfied trait bounds
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_rapier3d-0.29.0\src\dynamics\rigid_body.rs:88:1
|
88 | pub struct Velocity {
| ------------------- doesn't satisfy _: Component
|
= note: the full type name has been written to 'C:\Users\fer52\mynewcargo\my_project\target\debug\deps\my_project.long-type-7822903801038917473.txt'
= note: consider using --verbose to print the full type name to the console
= note: the following trait bounds were not satisfied:
bevy_rapier3d::dynamics::Velocity: bevy::prelude::Component
error[E0599]: the method single exists for struct Query<'_, '_, (&mut CameraState, &mut ..., ...), ...>, but its trait bounds were not satisfied
--> src\main.rs:106:27
|
106 | commands.entity(query.single().id()).insert(Transform {
| ^^^^^^ method cannot be called due to unsatisfied trait bounds
|
::: C:\Users\fer52.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_rapier3d-0.29.0\src\dynamics\rigid_body.rs:88:1
|
88 | pub struct Velocity {
| ------------------- doesn't satisfy _: Component
|
= note: the full type name has been written to 'C:\Users\fer52\mynewcargo\my_project\target\debug\deps\my_project.long-type-7822903801038917473.txt'
= note: consider using --verbose to print the full type name to the console
= note: the following trait bounds were not satisfied:
bevy_rapier3d::dynamics::Velocity: bevy::prelude::Component
Some errors have detailed explanations: E0277, E0425, E0599.
For more information about an error, try rustc --explain E0277.