搬运一批Bedrock wiki内容,完善翻译
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: Detecting Other Entities
|
||||
category: Tutorials
|
||||
title: 侦测其他实体
|
||||
category: 教程
|
||||
tags:
|
||||
- intermediate
|
||||
- 中级
|
||||
mentions:
|
||||
- ANightDazingZoroark
|
||||
- SmokeyStack
|
||||
@@ -12,210 +12,189 @@ mentions:
|
||||
- TheItsNameless
|
||||
---
|
||||
|
||||
You might have thought about making your entities fire an event when other entities are nearby. This article details the various known ways to do so.
|
||||
# 侦测其他实体
|
||||
|
||||
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
|
||||
|
||||
当需要让实体在附近存在其他实体时触发事件,本文将详细介绍多种已知实现方式。
|
||||
|
||||
## minecraft:entity_sensor
|
||||
|
||||
This is probably the most basic way to detect other entities. The main issues is it only accepts one entry and testing if the entity is out of range can be very tricky. Because it's an entity component, you can just place into your entity behavior file and edit the Minecraft filters. Here's a demonstration:
|
||||
这是最基础的侦测方式。主要限制是只能接收单一条目,且检测实体退出范围较困难。作为实体组件,可直接植入实体行为文件并配置Minecraft过滤器:
|
||||
|
||||
<CodeHeader>BP/entities/my_entity.json#components</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/my_entity.json#components]
|
||||
"minecraft:entity_sensor": {
|
||||
"sensor_range": 2.5, //this is for the radius in blocks it will detect other entities in
|
||||
"relative_range": false, //if true, the sensor range is additive on top of the entity's hitbox size
|
||||
"require_all": true, //if true, all nearby entities must pass the filter conditions for the event to send
|
||||
"minimum_count": 1, //minimum amount of entities required for the event to fire. by default, it's 1
|
||||
"maximum_count": 4, //maximum amount of entities required for the event to fire. by default it's -1, which means infinity
|
||||
"event_filters": { //you can put any filter you want here, the one that's being used in this example just detects players
|
||||
"sensor_range": 2.5, //检测半径(格子数)
|
||||
"relative_range": false, //若为true,检测范围会叠加实体碰撞箱
|
||||
"require_all": true, //若为true,所有邻近实体需通过过滤条件才会触发事件
|
||||
"minimum_count": 1, //触发事件的最小实体数量(默认1)
|
||||
"maximum_count": 4, //触发事件的最大实体数量(默认-1表示无限)
|
||||
"event_filters": { //自定义过滤器(本例检测玩家)
|
||||
"test": "is_family",
|
||||
"subject": "other",
|
||||
"value": "player"
|
||||
},
|
||||
"event": "event:on_player_detected" //the event that fires when all the conditions in event_filters are met
|
||||
"event": "event:on_player_detected" //条件满足时触发的事件
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
## `/execute`
|
||||
## `/execute` 命令
|
||||
|
||||
Using the new `/execute` command that has been introduced since 1.19.50, you can execute commands as long as another entity is nearby.
|
||||
使用1.19.50版本新增的`/execute`命令,可在附近存在实体时执行指令。以下示例使猪在检测到玩家时发出"oink oink"声(可自定义事件):
|
||||
|
||||
This example you'll be following will make pigs say "oink oink" upon detecting players, though you can replace those with whatever you want. First of all, copy-paste these BP animations.
|
||||
|
||||
<CodeHeader>BP/animations/detection_animation.json</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/animations/detection_animation.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
"animation.pig.find_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"/execute as @s if entity @e[type=player, r=4] run event entity @s wiki:player_detected"
|
||||
]
|
||||
}
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
"animation.pig.find_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"/execute as @s if entity @e[type=player, r=4] run event entity @s wiki:player_detected"
|
||||
]
|
||||
}
|
||||
},
|
||||
"animation.pig.find_no_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"/execute as @s unless entity @e[type=player, r=4] run event entity @s wiki:no_player_detected"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
首个动画用于检测实体存在,第二个检测实体离开。可通过`/event`命令添加[虚拟组件](/entities/dummy-components)或更新[实体属性](https://learn.microsoft.com/zh-cn/minecraft/creator/documents/introductiontoentityproperties)。
|
||||
|
||||
::: code-group
|
||||
```json [BP/animation_controllers/pig_animation_controllers.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animation_controllers": {
|
||||
"controller.animation.pig_find_player": {
|
||||
"initial_state": "default",
|
||||
"states": {
|
||||
"default": {
|
||||
"animations": ["find_player"],
|
||||
"transitions": [{
|
||||
"detected": "q.is_sheared"
|
||||
}]
|
||||
},
|
||||
"animation.pig.find_no_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"/execute as @s unless entity @e[type=player, r=4] run event entity @s wiki:no_player_detected"
|
||||
]
|
||||
}
|
||||
"detected": {
|
||||
"animations": ["find_no_player"],
|
||||
"transitions": [{
|
||||
"default": "!q.is_sheared"
|
||||
}],
|
||||
"on_entry": ["/say oink oink"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
The first one is for detecting if the entity is present, and the other for detecting if the entity is not present. The events used in the `/event` part of the `/execute` commands can be used for adding a [dummy component](/entities/dummy-components) or updating an [actor property](https://learn.microsoft.com/en-us/minecraft/creator/documents/introductiontoentityproperties).
|
||||
|
||||
Next of all, copy paste this BP animation controller. This assumes that you set up the `/event` parts of the `/execute` commands to add or remove `minecraft:is_sheared`.
|
||||
|
||||
<CodeHeader>BP/animation_controllers/pig_animation_controllers.json</CodeHeader>
|
||||
|
||||
```json
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animation_controllers": {
|
||||
"controller.animation.pig_find_player": {
|
||||
"initial_state": "default",
|
||||
"states": {
|
||||
"default": {
|
||||
"animations": ["find_player"],
|
||||
"transitions": [
|
||||
{
|
||||
"detected": "q.is_sheared"
|
||||
}
|
||||
]
|
||||
},
|
||||
"detected": {
|
||||
"animations": ["find_no_player"],
|
||||
"transitions": [
|
||||
{
|
||||
"default": "!q.is_sheared"
|
||||
}
|
||||
],
|
||||
"on_entry": ["/say oink oink"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
Finally, copy-paste this snippet into the behavior file for the pig-like so. Make sure to insert this in `description`.
|
||||
|
||||
<CodeHeader>BP/entities/my_entity.json#description</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/my_entity.json#description]
|
||||
"animations": {
|
||||
"manage_find_player": "controller.animation.pig_find_player",
|
||||
"find_player": "animation.pig.find_player",
|
||||
"find_no_player": "animation.pig.find_no_player"
|
||||
"manage_find_player": "controller.animation.pig_find_player",
|
||||
"find_player": "animation.pig.find_player",
|
||||
"find_no_player": "animation.pig.find_no_player"
|
||||
},
|
||||
"scripts": {
|
||||
"animate": [
|
||||
"manage_find_player"
|
||||
]
|
||||
"animate": ["manage_find_player"]
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
## Molang, BP Animations & Animation Controllers
|
||||
## Molang、动画与动画控制器
|
||||
|
||||
The `for_each` function and `q.get_nearby_entities` or `q.get_nearby_entities_except_self` can also be used for detecting other entities. They are more effective than using `minecraft:entity_sensor` because they are better at detecting if the entity you want to detect goes away than with `minecraft:entity_sensor`. The only downside is that they're experimental.
|
||||
使用`for_each`函数配合`q.get_nearby_entities`或`q.get_nearby_entities_except_self`可更高效检测实体(实验性功能),能更好处理实体离开的情况。
|
||||
|
||||
Just like in the previous method we will make pigs say "oink oink" upon detecting players, though you can replace those with whatever you want. First of all, copy-paste this BP animation:
|
||||
|
||||
<CodeHeader>BP/animations/detection_animation.json</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/animations/detection_animation.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
"animation.pig.find_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"v.x = 0.0; for_each(t.player, q.get_nearby_entities_except_self(16, 'minecraft:player'), { v.x = v.x + 1; }); return v.x > 0.0;"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
"animation.pig.find_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"v.x = 0.0; for_each(t.player, q.get_nearby_entities_except_self(16, 'minecraft:player'), { v.x = v.x + 1; }); return v.x > 0.0;"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
The first parameter that `q.get_nearby_entities_except_self` needs to work is the radius in blocks it will detect other entities in. The other is the identifier of the mob you want to make it detect.
|
||||
若要检测具备特定Molang属性的实体:
|
||||
|
||||
Now that's good and all, but on the off chance, you want to make the pig detect players with some attribute that can be detected with Molang, use this.
|
||||
|
||||
<CodeHeader>BP/animations/detection_animation.json</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/animations/detection_animation.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
"animation.pig.find_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"v.x = 0.0; for_each(t.player, q.get_nearby_entities_except_self(2, 'minecraft:player'), { v.x = v.x + (t.player -> q.is_sheared); }); return v.x > 0.0;"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
"format_version": "1.10.0",
|
||||
"animations": {
|
||||
"animation.pig.find_player": {
|
||||
"animation_length": 0.05,
|
||||
"loop": true,
|
||||
"timeline": {
|
||||
"0": [
|
||||
"v.x = 0.0; for_each(t.player, q.get_nearby_entities_except_self(2, 'minecraft:player'), { v.x = v.x + (t.player -> q.is_sheared); }); return v.x > 0.0;"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
Next of all, copy paste this BP animation controller:
|
||||
|
||||
<CodeHeader>BP/animation_controllers/pig_animation_controllers.json</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/animation_controllers/pig_animation_controllers.json]
|
||||
{
|
||||
"format_version": "1.10.0",
|
||||
"animation_controllers": {
|
||||
"controller.animation.pig_find_player": {
|
||||
"initial_state": "default",
|
||||
"states": {
|
||||
"default": {
|
||||
"animations": ["find_player"],
|
||||
"transitions": [
|
||||
{
|
||||
"detected": "v.x > 0"
|
||||
}
|
||||
]
|
||||
},
|
||||
"detected": {
|
||||
"animations": ["find_player"],
|
||||
"transitions": [
|
||||
{
|
||||
"default": "v.x <= 0"
|
||||
}
|
||||
],
|
||||
"on_entry": ["/say oink oink"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"format_version": "1.10.0",
|
||||
"animation_controllers": {
|
||||
"controller.animation.pig_find_player": {
|
||||
"initial_state": "default",
|
||||
"states": {
|
||||
"default": {
|
||||
"animations": ["find_player"],
|
||||
"transitions": [{
|
||||
"detected": "v.x > 0"
|
||||
}]
|
||||
},
|
||||
"detected": {
|
||||
"animations": ["find_player"],
|
||||
"transitions": [{
|
||||
"default": "v.x <= 0"
|
||||
}],
|
||||
"on_entry": ["/say oink oink"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
Finally, copy-paste this snippet into the behavior file for the pig-like so. Make sure to insert this in `description`.
|
||||
|
||||
<CodeHeader>BP/entities/my_entity.json#description</CodeHeader>
|
||||
|
||||
```json
|
||||
::: code-group
|
||||
```json [BP/entities/my_entity.json#description]
|
||||
"animations": {
|
||||
"manage_find_player": "controller.animation.pig_find_player",
|
||||
"find_player": "animation.pig.find_player"
|
||||
"manage_find_player": "controller.animation.pig_find_player",
|
||||
"find_player": "animation.pig.find_player"
|
||||
},
|
||||
"scripts": {
|
||||
"animate": [
|
||||
"manage_find_player"
|
||||
]
|
||||
"animate": ["manage_find_player"]
|
||||
}
|
||||
```
|
||||
:::
|
||||
Reference in New Issue
Block a user