0
\$\begingroup\$

Following this lecture: http://www.shmup-dev.com/forum/viewtopic.php?f=6&t=347 , i created a bullet that shoot from the enemy directly to the player, in a straight line.

Now i want to create 3 bullets that are shot from the enemy to the player creating a spread effect, one shot goes directly to the player, and the other two opens a little from the one of the middle,

This line of code is the one that create the line motion from the enemy to the player:

    // calculate the direction to the player
    shootVector = SharedInfo.getInstance().player.transform.position - myTransform.position;

How can i create the other two motions for the bullets?

http://postimg.org/image/ialuek24r/

Here is the code i have for one shoot (this code is applied to the enemy):

void Shoot() // waits for 'delay' seconds, then shoots directly at the player
{
    // get a bullet from the stack
    //var newBullet = gameManager.GetComponent(GameManager).enemyBulletStack.Pop();
    Bullet newBullet = (Bullet)SharedInfo.getInstance().enemyBulletStack.Pop();

    // position and enable it
    newBullet.gameObject.transform.position = new Vector3 (myTransform.position.x,
                                                           myTransform.position.y,
                                                           myTransform.position.z);
    newBullet.gameObject.SetActive(true);

    if (changeShootVector) {
        // calculate the direction to the player
        shootVector = SharedInfo.getInstance().player.transform.position - myTransform.position;
    }

    // normalize this vector (make it length 1)
    shootVector.Normalize();
    shootVector = new Vector3 (0, shootVector.y, shootVector.z);

    // scale it up to the correct speed
    shootVector *= enemyBulletSpeed;
    newBullet.motion = shootVector;
}
\$\endgroup\$
1
  • \$\begingroup\$ in the code myTransform is just a cache of transform, changeShootVector is a boolean to shoot multiple times to the same direction, also the camera is a ortographic one looking to the right, that's why the z is the x coordinate in the code \$\endgroup\$
    – fracedo
    Apr 1, 2015 at 3:56

1 Answer 1

0
\$\begingroup\$

You have options:

  1. If all 3 bullets should hit the target within a certain radius, add a small random amount, in world-units, to a random player-local axis (X or Y), when calculating the angle.

    shootVector = player.pos + randomDriftInWorldUnits - myTransform.pos;

  2. If the 2 extra bullets can spread too far apart to hit the player, even if the center bullet does, add a small random deflection to shootVector after calculating it. Rotating a bullet on its local Y-axis produces horizontal drift; RotationX produces vertical. A value <0.5 degrees would be a good starting point.

    shootVector *= Matrix.CreateRotationX(randomVerticalDeflection);
    shootVector *= Matrix.CreateRotationY(randomHorizontalDeflection);
    shootVector.Normalize();

The last code is overly-simplified, the rotations need to be added to the bullets initial orientation, which you are not, currently, using. Basically, you can multiply a unit-forward vector by (bulletInitialRotationMatrix * deflectionRotationMatrix * bulletInitialTranslationMatrix).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .