#make an entity move forwards

1 messages · Page 1 of 1 (latest)

ocean atlas
#

in trying to make a projectile always move forwards(when I summon a projectile) how do I do that?

hybrid vine
#

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

obsidian temple
hybrid vine
obsidian temple
obsidian temple
hybrid vine
#

Use get ViewDirection and then calculate the dx dy dz and move it my dx dy dz a little bit each time

obsidian temple
hybrid vine
#

or just use applyKnockback

obsidian temple
hybrid vine
#

coz otherwise u gotta code collision as well

obsidian temple
#

Entity. What

hybrid vine
obsidian temple
#

Ohhhh

#

That would be buggy

#

Will stick to knockback

hybrid vine
ocean atlas
hybrid vine
ocean atlas
#

that will make the projectileb always move forwards?

hybrid vine
#

no

#

that will get the view direction

ocean atlas
#

how do I make it always move forwards , im confused

hybrid vine
#

you need to use some maths with vec3's

#

.getViewDirection() returns a vec3

ocean atlas
#

Idk how to do that

hybrid vine
#

umm

#

dms?

ocean atlas
#

uh sure

drowsy haven
#

Just use applyImpulse

#

I did so for my custom dragon behavior

ocean atlas
drowsy haven
#

@ocean atlas wait how are you summoning the projectile?

dark shale
drowsy haven
dark shale
#

the player

drowsy haven
#

That doesn't really help...

sage tundra
#

what event are you using

drowsy haven
sage tundra
# ocean atlas how
// 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);
dark shale
#
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

sage tundra
dark shale
#

I stored it in variables

sage tundra
#
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

dark shale
#

Sometimes it bugged, I don't know why, after I added clearVelocity() it went desbugged.

#

Sorry for the English, I'm Brazilian

ocean atlas
ocean atlas
dark shale
ocean atlas
#

the entity is called stek:fire ?

ocean atlas
dark shale
ocean atlas
dark shale
#

the important thing is to execute the function

ocean atlas
# dark shale 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;
    }
})```
dark shale
#

I didn't understand what you wanted to do

ocean atlas
#

gonna use commands to summon the entity and a separate script to make the entity go forwards

ocean atlas
#

there's nothing wrong with it , in good with commands I just want to know if the script works