# Direction player looks particle

Comments in 'Plugin Development' started by RoboDaim, Aug 21, 2016.

1. Offline

### RoboDaimNew Member

Joined:
Aug 17, 2016
Posts:
5
Minecraft User:
how to create a line of particles where the player is looking?
PHP:
``` for(\$min= 0;\$min<40;\$min++){//spawning particle} ```
2. Offline

### MyNameIsTriXzNotable Member

Joined:
Aug 17, 2015
Posts:
538
Minecraft User:
PHP:
``` \$x = \$player->getDirectionVector()->x \$y = \$player->getDirectionVector()->y;\$z = \$player->getDirectionVector()->z;\$px =  \$player->x;\$py = \$player->y;\$pz = \$player->z;for(\$i = 0; \$i < 40; \$i++){\$level->addParticle(new FlameParticle(new Vector3(\$i * \$x + \$px, \$i * \$y + \$py, \$i * \$z + \$pz)));} ```
3. Offline

### MyNameIsTriXzNotable Member

Joined:
Aug 17, 2015
Posts:
538
Minecraft User:

IN PRACTICE:

Steve has a Yaw of 90 and a Pitch of 0.
Steve's coordinates are 100, 4, 100.

\$x will be 0;
\$y will be 0;
\$z will be 1.0;

\$px = 100;
\$py = 4;
\$pz = 100;

Particlespawn1: 100, 0, 101
Particlespawn2: 100, 0, 102
And so on...

Practice test 2:

Steve has a Yaw of 135 and a Pitch of 45.
Steve's coordinates are 100, 4, 100.

\$x will be -0.5;
\$y will be 0.5;
\$z will be 0.5;

\$px = 100;
\$py = 4;
\$pz = 100;

Particlespawn1: 99.5 4.5 100.5
Particlespawn2: 99 5 101
And so on
---------------------------

To understand how Player::getDirectionVector() works,
It uses a cosinus, sinus formula which is used in most coordinates based 3D games to moves things to their direction. You can get the Vector by that code too:

PHP:
``` \$yaw = \$player->yaw;\$pitch = \$player->pitch;\$y = \$yaw + 45 * M_PI / 180;\$p = \$pitch * M_PI / 180 * -1;\$v3 = new Vector3;\$v3->x = cos(\$y) * cos(\$p);\$v3->y = sin(\$p);\$v3->z = sin(\$y) * cos(\$p);return \$v3; ```
4. Offline

### SOFeBanned

Joined:
May 28, 2016
Posts:
386
Minecraft User:
In practice, it is probably more effective to add the unit vector every time:

PHP:
``` function(Player \$player, Particle \$particle){  \$position = \$player;  \$unitVector = \$player->getDirectionVector();  for(\$i = 0; \$i < 40; \$i++){    \$position = \$position->add(\$unitVector);    \$particle->setComponents(\$position->x, \$position->y, \$position->z);    \$player->getLevel()->addParticle(\$particle);  }} ```
Last edited: Aug 25, 2016
5. Offline

### Nawaf_Craft1bActive MemberPlugin Developer

Joined:
May 8, 2015
Posts:
232
Plugins:
1
Minecraft User:
PHP:
``` function(Player \$player, Particle \$particle){  \$position = \$player;  \$unitVector = \$player->getDirectionVector();  for(\$i = 0; \$i < 40; \$i++){    \$position = \$position->add(\$unitVector);    \$particle->setComponents(\$position->x, \$position->y, \$position->z);    \$player->getLevel()->addParticle(\$particle);  }} ```
iDirtPlayzMC likes this.
6. Offline

### SOFeBanned

Joined:
May 28, 2016
Posts:
386
Minecraft User:
Thanks, but next time, it would be much better if you point out the typo directly rather than copying the code and correcting it, which it would be troublesome to compare what's changed.
7. Offline

### Nawaf_Craft1bActive MemberPlugin Developer

Joined:
May 8, 2015
Posts:
232
Plugins:
1
Minecraft User:
okey