Skip to content

Implement weapon systems

Lee Seokhyun edited this page Jul 30, 2020 · 9 revisions

In this chapter, you will learn.

  • How to implement weapon systems.

Weapons and Bullets

This is the first chapter of battle systems. Let's first look at the structure of the weapon system object and see how bullets and weapons scripts work together. Open the weapons object at the bottom of the Player object. You can see the four-game objects Weapon01, Weapon02, Weapon03, and Weapon04.

Weapons

Each object implements its weapons. Usually, one is activated, but if you want to operate several at the same time, you can activate that much. The weapon systems fire bullets by their feature. Below is the property of it.

image

This weapon class defines the below features.

  • Where to shot.
  • How fast it can be shot.
  • How many bullets to be fired at once.
  • The power of bullets.

You can make various weapons to modify the above properties through the inspector.

private void OnEnable()
{
    StartCoroutine(FireLoop());
}


IEnumerator FireLoop()
{
    GameObject player = GameObject.Find("Player");

    while (true)
    {
        yield return new WaitForSeconds(this.interval);

        // Instantiate a bullet, position.
        Bullet bullet = GameObject.Instantiate(this.prefabBullet);
        bullet.transform.position = transform.position;
        bullet.transform.rotation = transform.rotation;

        // Set bullet properties.
        bullet.SetDirection(transform.forward);
        bullet.SetSpeed(this.speed);

        // Calculate fire direction.
        if (this.toPlayer)
        {
            if (player != null)
            {
                this.direction = (player.transform.position - transform.position).normalized;
                bullet.transform.rotation = Quaternion.LookRotation(this.direction);
                bullet.SetDirection(this.direction);
            }
        }

        // Missile.
        if (this.isMissile)
        {
            bullet.SetFollowTarget();
        }
    }
}

This is the weapon script. The FireLoop method is the main code of it. Both the Player and the Enemy use this class. The this.toPlayer variable is a variable used for enemy weapons. Used to implement weapons directed at the player. This.isMissile variable refers to the type of missile that tracks the enemy.

Bullets

Normal bullets move one direction after they had fired. If it is a missile then it will follow the target automatically.

void Update()
{
    DetermineMissileTarget();
    DetermineDirection();
    Move();
    CheckArea();
}

See the Update method above. First, it determines the target if this type is a missile. The code is here. You can get how to find the target from the comments.

void DetermineMissileTarget()
{
    // If this is a missile...
    if (this.isFollowTarget)
    {
        // If this has no target yet
        if (this.followTarget == null)
        {
            if (Random.Range(0, 100) < 50)
            {
                // Find the farthest enemy with a 50% chance.
                this.followTarget = FindFarthestEnemy();
            }
            else
            {
                // Find the nearest enemy with a 50% chance.
                this.followTarget = FindNearestEnemy();
            }
        }
    }
}

Second, it determines which direction to be fired.

void DetermineDirection()
{
    if (this.followTarget != null)
    {
        // Calculate rotation to target.
        this.direction = (this.followTarget.position - transform.position).normalized;
        Quaternion targetRotation = Quaternion.LookRotation(this.direction);

        // This variable represents the quality of the missile.
        // If this value is small, it turns very slowly.
        // If this value is large, it turns very fast to target.
        float angularVelocity = 4.0f;
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * angularVelocity);
    }
}

Third, it moves the bullet to the calculated direction.

void Move()
{
    Vector3 velocity = transform.forward * this.speed;
    transform.position += Time.deltaTime * velocity;
}

Last, it removes the bullet when it is off the screen.

void CheckArea()
{
    // It is destroy when position is over.
    if (transform.position.z >= 10.0f ||
        transform.position.z <= -10.0f ||
        transform.position.x >= 6.0f ||
        transform.position.x <= -6.0f)
    {
        Destroy(gameObject);
    }
}

Conclusion

The weapon systems are an essential part of most of the shooting games. This is the very basic implementation but you might develop more powerful weapons based on this code. Next, we will learn about our enemies. Let's go.