Skip to content

Commit 62c64ff

Browse files
authored
player impulse (JaylyDev#173)
1 parent 334eb67 commit 62c64ff

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

scripts/player-impulse/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# player-impulse
2+
3+
## Description
4+
5+
Remake `setVelocity` for players.
6+
7+
## Usage
8+
9+
```js
10+
import { applyImpulse } from "./index";
11+
12+
applyImpulse(player, vector);
13+
```
14+
15+
## Credits
16+
17+
This script is made by [SI Silicon](https://github.com/SIsilicon), updated by Remember M9#8416 and Usernam#2058

scripts/player-impulse/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Script example for ScriptAPI
2+
// Author: Usernam#2058 <Jayly Discord>
3+
// Remember M9#8416 <Bedrock Add-Ons>
4+
// SI Silicon <https://github.com/SIsilicon>
5+
// Project: https://github.com/JaylyDev/ScriptAPI
6+
7+
import { Player, Vector3 } from "@minecraft/server";
8+
9+
/**
10+
* Workaround to apply impulse vector to the current velocity of the player.
11+
* @param player Only Player class, not Entity.
12+
* @param vector
13+
*/
14+
export function applyImpulse(player: Player, vector: Vector3) {
15+
const { x, y, z } = vector;
16+
const horizontal = Math.sqrt(x * x + z * z) * 2.0;
17+
const vertical = y < 0.0 ? 0.5 * y : y;
18+
player.applyKnockback(x, z, horizontal, vertical);
19+
}

scripts/player-impulse/tests.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Script example for ScriptAPI
2+
// Author: Usernam#2058 <Jayly Discord>
3+
// Project: https://github.com/JaylyDev/ScriptAPI
4+
5+
import { applyImpulse } from "./index";
6+
import { Vector3, world } from "@minecraft/server";
7+
8+
const armorStand = [...world.getDimension("overworld").getEntities({ type: 'minecraft:armor_stand' })][0];
9+
const player = world.getAllPlayers()[0];
10+
const vector: Vector3 = { x: 0, y: 2, z: 0 };
11+
12+
armorStand.applyImpulse(vector);
13+
applyImpulse(player, vector);

0 commit comments

Comments
 (0)