This tutorial will cover how to make a hit or miss system. It is of intermediate difficulty and from my testing, prone to some minor weirdness that I haven't completly worked out.I'm currently working on implementing this for the map "Dark Shooter RPG" being developed by Team Magic.
The basic premise is that you can create weapons that have a chance to hit or miss. If it misses, you can have the projectile attack a point within a fixed radius of the intended target and apply damage if an enemy unit is at that point. This could also be used for many things like spells and abilities. In the future I will be exploring ways to have a unit be upgraded which will effect their damage and hit or miss chances.
Here is the step by step instructions for implementing this as a companion to the video:
Now the nitty gritty
Go to Data Editor and bring up Effect tab
1. First, we want to set up our custom poison pistol damage effect. If our attack hits a unit, this will be applied.
Add Effect ->
Name: "Poison Pistol - Damage"
Effect Type: Damage
For now leave the actual values for the effect as the default. This can be tweaked later.
2. Now we need to create a Search Effect. This will allow us to check a point near the target to see if it is a unit so we can apply damage as needed.
Add Effect ->
Name: "Poison Pistol - Search"
Effect Type: Search Area
Edit your new effect
Edit "Search - Areas +"
Set Effect = "Poison Pistol - Damage"
Set Maximum Count = 1
Set Radius = 1 (This is the size of the point to search. A bullet is small so 1 is good.)
Set Target - Launch Location + = Target Unit/Point
Edit Target - Marker +
Set Mismatch flags -> Casting Unit and Casting Player = Enabled (We don't want the search to hit us.)
3. Create a launch missile effect. We will tie our search area effect to trigger on impact.
Add Effect ->
Name: "Poison Pistol - Launch"
Effect Type: Launch Missile
Field Values: Copy From: Hydralisk - Needle Spine (Launch Missile)
Now edit you new effect
Set Effect - Impact Effect = "Poison Pistol - Search"
Set Target = Impact Location + = Target Unit/Point
4. Now we want to create a Persistant Effect which will allow us to offset a missed shot by a random x,y value from the intended target.
Add Effect ->
Name: "Poison Pistol - Persistent"
Effect Type: Create Persistent
Now edit the persistent:
Effect - Flags = Random Offset (checked)
Effect - Height Map = Ground
Effect - Period Effects = Add Value -> Poison Pistol - Launch
Effect - Period Offsets = ((1.5,0,0)|(0,1.5,0)|(-1.5,0,0)|(0,-1.5,0)|(1.06,1.06,0)|(1.06,-1.06,0)|(-1.06,1.06,0)|(-1.06,-1.06,0)) X,Y coordinates for where you want your shots to land if they miss. In my case, I want shots that miss to go a certain radius from the intended target. Since we can't have a random value around a radius, we have to input the offsets manually which means it's time for some math. You can accept these values to do 8 points on a circle of radius 1.5 or read this spoiler for the explanation and how to do a custom radius.
Target - Location + (None):Target Unit/Point (We want the target location to be at our offset)
Target - Location Offset - End + (None):Target Unit/Point
How to get the x,y coordinates for a given radius at a given angle:
A detailed explanation is available at http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
For our calculations, we will use some simple trigonometric functions -
point x = radius * cos(angle)
point y = radius * sin(angle)
The angle always starts with a line from the center to the right side with an angle of 0 degrees and moves counter-clockwise.
90 degrees is to the top, 180 degrees to the left side, and 270 degrees to the bottom. Any angle in-between those 90 degree increments can be interpolated.
The radius is how far from the original target unit you want the projectile to deviate. In my example, I want a radius of 1.5, so let's calculate some points.
x = 1.5 * cos(0) = 1.5
y = 1.5 * sin(0) = 0
So our first point will be (1.5, 0) which is the right side of the circle. To get the left side we just flip that and have (-1.5, 0).
For the top and bottom point we get:
x = 1.5 * cos(90) = 0
y = 1.5 * sin(90) = 1.5
Our next points will be (0,1.5) and (0,-1.5) for the top and bottom points.
Let's also do the diagonals of the circle.
x = 1.5 * cos(45) = 1.06
y = 1.5 * sin(45) = 1.06
So from this, the top right diagonal of the circle is (1.06,1.06). We can then assume that the left diagonal is (-1.06,1.06). Do you see the pattern yet?
Once you have all of your desired strike points calculated ( you can go in as small increments as you want. For this we will only do increments of 45 degrees).
5. Now we want to create a Set Effect that will allow us to randomly choose between a hit or miss.
Add Effect ->
Name: "Poison Pistol - Attack"
Effect Type: Set
Now edit the set:
Effect - Effects: Add both the "Poison Pistol - Damage" and "Poison Pistol - Persistent" (In my case, I want a 66% chance to miss and a 33% chance to hit, so I add 1 damage value and 2 miss values. This can be tweaked to your wanted hit/miss percentage.)
Effect - Maximum Count = 1
Effect - Minimum Count = 1 (We always want a single effect chosen.)
Effect - Random = Enabled (Choose one effect at random.)
6. Now we want to add our new hit/miss to an actor to have the launch animations happen.
Go to Actor tab and select "Hydralisk Attack"
Set Token - Attack Effect = "Poison Pistol - Attack"
Set Token - Impact Effect = "Poison Pistol - Search"
Set Token - Launch Effect = "Poison Pistol - Launch"
7. Now we want to edit our hydralisk weapon to use our attack set.
Go to Weapon tab and select "Hydralisk - Needle Spines"
Set Effect - Effect = "Poison Pistol - Attack"
At this point our system should be in place minus some minor tweaking with actors/animation. Go ahead and test it.
If you have any problems, please post and I'll try to find help. I also welcome any feedback for how to make this better (tutorial or the system itself) so please leave a reply :)
This would definitely succeed in making projectiles look more naturally fired. I don't think I would have too much use for the miss as it doesn't make too much sense to me. Usually people don't miss except at large ranges. Although I more of skimmed the tutorial, it would be interesting to see the projectile have a greater chance to miss at larger ranges.
Although, about the marine's projectile. Naturally, people don't aim down so they don't necessarily hit the ground when they miss. Although I wouldn't need to normally worry about this, I think it would go really great hand in hand with my above suggestion and make the effect a bit more realistic.