Adding Components To A Projectile

A simple explanation of how to add components to a projectile.

Components are the code through which a projectile can achieve many interesting effects that are simply not possible through modifying its stats alone. There is a multitude of base game components, and making custom components is also possible to achieve effects that the base game code alone cannot.

Adding the Component

Here is an example of a fake component being added to a projectile, using the GetOrAddComponent method.

ExampleComponent componentReference = projectile.gameObject.GetOrAddComponent<ExampleComponent>();

Obviously ExampleComponent isn't real, so using this exact code won't do anything, but there are plenty of simple base game components that will be demonstrated in the following section.

This method returns a reference to the component in the form of the componentReference variable. This will allow you to adjust various variables in the component (for example, the number of times a bouncing component will allow a projectile to bounce).

If the component you are trying to add already exists on the projectile, componentReference will instead be a reference to the already existing component.

Useful Components

Bouncing

The name of the Bouncing component is 'BounceProjModifier'. To add bouncing to a projectile, you can do the following;

BounceProjModifier bounce = projectile.gameObject.GetOrAddComponent<BounceProjModifier>();
bounce.numberOfBounces += 3;

That's all you need to make a projectile bounce. The number you put next to numberOfBounces will determine how many times the projectile is allowed to bounce, as its name implies. However, numberOfBounces is far from the only variable in the bounce component.

  • damageMultiplierOnBounce - Upon bouncing, the projectile's damage is multiplied by this number. For example, if this number is set to 1.5, then the projectile will gain a 50% damage bonus upon bouncing.

  • bouncesTrackEnemies - If this is set to true, instead of bouncing at an angle that makes sense for their current direction, projectiles will instead bounce towards the nearest enemy.

  • TrackEnemyChance - This number determines how likely bouncing projectiles are to bounce towards an enemy. Does nothing if bouncesTrackEnemies is false. The chance goes from 0 to 1, with 0 being a 0% chance and 1 being a 100% chance. 0.5, for example, is a 50% chance to track on bounce.

  • bounceTrackRadius - This number determines how close an enemy will have to be for a tracking bounce to try and target them. It is measured in tiles, so a bounceTrackRadius of 7 will cause a projectile to only attempt to bounce towards enemies that are within 7 tiles.

All these variables can be adjusted in the same way as numberOfBounces, for example bounce.damageMultiplierOnBounce = 1.6f;.

Last updated