飞行实体控制
intermediate
无论是制作飞机还是飞龙,为飞行实体添加可控性对于没有接触过这个概念的开发者来说可能是个挑战。由于没有"唯一正确"的方法来实现飞行控制,我将展示三种主要的实现方式。
高跳慢落法
虽然不算真正的"飞行",但通过设置实体高跳跃能力并赋予下落时的缓降和加速效果,这是最直接的方法。
首先需要为实体添加"minecraft:horse.jump_strength"组件来控制跳跃力度,并禁用玩家跳跃时的下马动作。
json
"minecraft:horse.jump_strength": {
"value": 7
}也可以使用范围值来显示玩家按住跳跃键时的蓄力条:
json
"minecraft:horse.jump_strength": {
"value": { "range_min": 0.6, "range_max": 1.2 }
}接着通过动画控制器在实体离地时赋予缓降和加速效果:
json
"controller.animation.dragon.flying":{
"states":{
"default":{
"transitions":[
{"jumping":"!q.is_on_ground"}
]
},
"jumping":{
"transitions":[
{"default":"q.is_on_ground"}
],
"on_entry":[
"/effect @s slow_falling 20000 0 true",
"/effect @s speed 20000 10 true"
],
"on_exit":[
"/effect @s clear"
]
}
}
}最后将控制器绑定到实体:
json
"description":{
"identifier":"wiki:dragon",
"scripts":{
"animate":["flying"]
},
"animations":{
"flying":"controller.animation.dragon.flying"
}
}视角控制法
这是最流行的飞行控制方式,通过检测玩家垂直视角来调整实体高度。缺点是视角移动会影响飞行轨迹。
使用命令方块检测玩家俯仰角并施加相应效果:
execute as @a[rxm=-90,rx=-25] run effect @e[type=wiki:dragon,r=1] levitation 1 6 true
execute as @a[rxm=-25,rx=-15] run effect @e[type=wiki:dragon,r=1] levitation 1 3 true
execute as @a[rxm=-15,rx=-5] run effect @e[type=wiki:dragon,r=1] levitation 1 2 true
execute as @a[rxm=-5,rx=20] run effect @e[type=wiki:dragon,r=1] levitation 1 1 true
execute as @a[rxm=20,rx=35] run effect @e[type=wiki:dragon,r=1] slow_falling 1 1 true
execute as @a[rxm=35,rx=90] run effect @e[type=wiki:dragon,r=1] clear注意根据实体大小调整选择器半径r值
配合动画控制器实现持续加速:
json
"controller.animation.dragon.flying":{
"states":{
"jumping_1":{
"on_entry":["/effect @s speed 15 10 true"],
"transitions":[
{"transition_to_default":"q.is_on_ground"},
{"jumping_2":"true"}
]
}
}
}添加骑乘标签检测防止误触发:
json
"controller.animation.dragon.test_rider":{
"states":{
"has_rider":{
"on_entry":["/tag @s add has_rider"],
"on_exit":["/tag @s remove has_rider"]
}
}
}跳跃控制法
第三种方法通过跳跃键控制升降。禁用默认跳跃和下马行为:
json
"minecraft:horse.jump_strength": {"value": 0},
"minecraft:can_power_jump": {}创建响应跳跃输入的动画控制器:
json
"controller.animation.fly_dragon":{
"states":{
"rising":{
"on_entry":["/effect @e[type=wiki:dragon,r=1,c=1] levitation 100000 6 true"],
"transitions":[
{"falling":"!q.is_jumping"}
]
}
}
}修改玩家行为文件添加控制器:
json
"description":{
"animations":{"fly_dragon":"controller.animation.fly_dragon"},
"scripts":{
"animate":[{"fly_dragon":"q.is_riding"}]
}
}添加防bug重置机制:
json
"controller.animation.reset_levitation":{
"states":{
"has_rider":{
"on_exit":["/effect @s levitation 0"]
}
}
}脚本控制法
第四种方法通过JavaScript脚本实现更精确的飞行控制。首先配置实体组件:
minecraft:entity
json
"components": {
"minecraft:type_family": {
"family": ["wiki:can_fly"]
}
}创建飞行系统工具类:
BP/scripts/utils.js
js
class Utils {
flySystem(flySpeed, fallSpeed, XZspeed) {
const direction = {
y: this.player.isJumping ? flySpeed : fallSpeed
};
this.entity.applyImpulse(direction);
}
}主程序循环执行控制:
BP/scripts/index.js
js
system.runInterval(() => {
for (const entity of dim.getEntities({families:["wiki:can_fly"]})) {
new Utils(entity).flySystem(0.09, 0.07, 5);
}
});可调整参数说明:
- 上升速度(flySpeed): 0.09
- 下降速度(fallSpeed): 0.07
- 水平速度(XZspeed): 5









