Spawn a projectile.

Discussion in 'Development' started by DiamondGamer30, Feb 11, 2019.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    168
    GitHub:
    diamondgamermcpe
    How can I spawn a projectile when a player clicks on air. My spawns but it doesn't move and the projectile actually hits the player.



    Here is my code:

    PHP:
    $nbt Entity::createBaseNBT($player->add(0$player->getEyeHeight(), 0), $directionVector$player->yaw$player->pitch);
    $nbt->setInt(Ammo::DAMAGE$this->damage);
    $entity Entity::createEntity(Ammo::class, $player->getLevel(), $nbt$player);
    $entity->setMotion($entity->getMotion()->multiply(50));
    if(
    $entity instanceof Projectile) {
        
    $event = new ProjectileLaunchEvent($entity);
        
    $event->call();
        if(
    $event->isCancelled()) {
            
    $entity->kill();
        }
        else {
            
    $entity->spawnToAll();
            
    $player->getLevel()->addSound(new LaunchSound($player), $player->getViewers());
        }
    }
    else {
        
    $entity->spawnToAll();
    }


    Ammo is a projectile entity I've registered. It is basically a shulker bullet. Nothing special, it extends the Projectile class.
     
  2. KielKing

    KielKing Zombie

    Messages:
    271
    GitHub:
    kielking
    projectile has no motion before it is spawned, I recommend you use a geometric formula to figure out what motion to give to the projectile to hit the aimed block
     
  3. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    168
    GitHub:
    diamondgamermcpe
  4. KielKing

    KielKing Zombie

    Messages:
    271
    GitHub:
    kielking
  5. wolfdale

    wolfdale Zombie

    Messages:
    343
    Actually he already defined the motion as $directionVector when calling createBaseNBT which i assume is the player's direction vector
    The issues is that the projectile is inside the bounding box of the player, so on the next tick it would be detected as having hit the player
    Spawn the projectile infront of the player instead
    PHP:
    //$directionVector = $player->getDirectionVector();
    $nbt Entity::createBaseNBT($player->add($directionVector->x$player->getEyeHeight(), $directionVector->z), $directionVector$player->yaw$player->pitch);
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.