#make an entity move forwards
1 messages · Page 1 of 1 (latest)
Use get ViewDirection and then calculate the dx dy dz and move it my dx dy dz a little bit each time, when I’m home I’ll send a script
yes
Wait, can you move any entity with script-api
i think so
That means we can make real car racing in minecraft
How
Use get ViewDirection and then calculate the dx dy dz and move it my dx dy dz a little bit each time

or just use applyKnockback
I mean what is the operation
coz otherwise u gotta code collision as well
Entity. What
i just used tryteleport
nah it works rlly well with no performance loss
how do I begin with viewdirection
player.getViewDirection()
that will make the projectileb always move forwards?
how do I make it always move forwards , im confused
Idk how to do that
uh sure
how
@ocean atlas wait how are you summoning the projectile?
player.dimension.spawnEntity('id:name', player.location)
Okay but what is triggering that?
the player
That doesn't really help...
what event are you using
Is the player using an item or something?
// Assuming you have a player stored to `player`
// Assuming you already summoned your entity and stored it to `entity`
const dir = player.getViewDirection();
entity.applyImpulse(dir);
note: this will only apply once so it will be affected by gravity and such, if you need to make the effect last longer, you'll need a runInterval
if you're using a projectile, it would be better to use the projectile component with shoot instead of applyImpulse
const dir = player.getViewDirection();
const projComp = entity.getComponent("projectile");
projComp.shoot(dir);
function shooter(entity, projetil, launcher) {
const velocity = {
x: entity.getViewDirection().x * launcher,
y: entity.getViewDirection().y * launcher,
z: entity.getViewDirection().z * launcher
};
const location = {
x: entity.location.x + entity.getViewDirection().x * 5,
y: entity.location.y + 1,
z: entity.location.z + entity.getViewDirection().z * 5
};
let proj = entity.dimension.spawnEntity(projetil, location);
proj.clearVelocity();
proj.getComponent('minecraft:projectile').shoot(velocity);
proj.getComponent('minecraft:projectile').owner = entity;
}
world.afterEvents.itemUse.subscribe((event) => {
const item = event.itemStack;
const player = event.source;
if (item.typeId == 'stek:fire_gun') {
shooter(player, 'stek:fire', 2)
}
})
if the entity has the 'projectile' component, this works
launches a projectile from the player's view
I advise storing repetitive things to variables, calling APIs like that has overhead. It's usually not a problem for small things, but it can have a heavy performance impact at scale
I stored it in variables
function shooter(entity, projetil, launcher) {
const entityLocation = entity.location;
const viewDirection = entity.getViewDirection();
const velocity = {
x: viewDirection.x * launcher,
y: viewDirection.y * launcher,
z: viewDirection.z * launcher
};
const location = {
x: entityLocation.x + viewDirection.x * 5,
y: entityLocation.y + 1,
z: entityLocation.z + viewDirection.z * 5
};
let proj = entity.dimension.spawnEntity(projetil, location);
const projComp = proj.getComponent('minecraft:projectile');
projComp.shoot(velocity);
projComp.owner = entity;
}
clearVelocity is also unneccessary as the entity just spawned and has no velocity
Sometimes it bugged, I don't know why, after I added clearVelocity() it went desbugged.
Sorry for the English, I'm Brazilian
I want to summon the entity with commands and use scripts to launch forwards
what is the shooter(player,'stek:fire',2) for ?
launches the entity
the entity is called stek:fire ?
With commands?
commands are easier for me , I only want the entity to be launched forwards even if only once
yes
if I don't use the itemuse.subscribe stuff will the projectile shoot forwards when it is summoned ?
the important thing is to execute the function
will this work ?
import { Entity } from '@minecraft/server';
function shooter(entity, projetil, launcher) {
const velocity = {
x: entity.getViewDirection().x * launcher,
y: entity.getViewDirection().y * launcher,
z: entity.getViewDirection().z * launcher
};
const location = {
x: entity.location.x + entity.getViewDirection().x * 5,
y: entity.location.y + 1,
z: entity.location.z + entity.getViewDirection().z * 5
};
let proj = entity.dimension.spawnEntity(projetil, location);
proj.clearVelocity();
proj.getComponent('op:projectileslash_bullet').shoot(velocity);
proj.getComponent('op:projectileslash_bullet').owner = entity;
}
})```
I didn't understand what you wanted to do
gonna use commands to summon the entity and a separate script to make the entity go forwards
Let me see your command
there's nothing wrong with it , in good with commands I just want to know if the script works