(connected to the previous article)
Described above in the trajectory implementation, about the u3d of the physical engine of some of the relevant points, and with the solid bullets to the key implementation code.
In this article, we will continue to illustrate the implementation of radial trajectory.
Iv. Radial Type Bullets
This chapter first discusses the bullet collision logic realization, because the radial type bullets with the U3d sprite is not drawn out, all need special skill, the drawing method is explained in the next chapter.
Non-automatic collision detection using the PHYSIC2D library
Unlike solid bullets, in the game I need to implement an attack type similar to the laser Ray, range damage (AOE).
In this case, you can not rely on rigidbody to achieve collision detection, the previous article said that the U3d physics engine does not support two collider collision detection.
Therefore, there is no way to "automatically" do collision detection (the so-called automatic, is to implement a function, and then wait for the u3d to automatically call when the collision occurs)
I still use the U3d 2D Physical library to detect the target collider in each frame (each update).
For example, AOE damage:
Raycasthit2d[] hits = Physics2d.circlecastall (transform.position, M_info.aoeradius, Vector2.zero,
Layermanager.getlayer (m_faction). Oppunitmask);
Use Physics2d's Circlecastall, Raycastall, and Boxcastall to perform a round, radial, and rectangular collision detection on all collider in the specified layer.
That is, the collider and collider are not used here, but the specified circle, ray, Rectangle, and which collider collide (Cast), including intersection and containment, are judged.
These functions have an all and a non-all version, which returns all detected collider or the nearest collider.
The return value Raycasthit contains collider, point (collision points), normal (collision normals), etc., please refer to U3d API, which is no longer described here.
Like the collider collision detection described above, the code implementation of the collision is placed in the update, and there may be "too many" or "through" situations.
However, because the demand itself is aimed at non-physical bullets, the area, the range is much larger than the bullet, all without too serious impact.
(if placed in the fixedupdate will be much more accurate, but the consumption is too large).
Implementation of attack logic for several ballistic types
Penetrating laser
Penetrating the laser can cause damage to units on the beam.
The implementation code for its attack:
IEnumerator _takeattack () {yield return Newwaitforseconds (ray_take_attack);Vector3 v=utils.up (transform); Vector3 Worldcenter= transform.position + v * m_info.attackdistance/2; Vector2 size=NewVector2 (width, m_info.attackdistance); Raycasthit2d[] Hits=Physics2d.boxcastall (worldcenter, size, Utils.dirtoangle (v),NewVector2 (0,0),mathf.infinity,Layermanager.getlayer (m_faction). Oppunitmask);
M_filter.clear ();
for (int s = 0; s < hits. Length; ++s)
{
Targetpick pick = Targetpick.from (ref hits [s]);
Pick. Toshield ();
if (pick)
if (M_filter.test (ref pick))
M_info.attackon (pick, V, m_myunit,hiteffecttype,null);
}
}
Note that the attack uses Startcoroutine to do a delay, this is because, in order to animate more realistic, when doing the laser painting, there is a fast laser beam length of the process, so the actual effect of the attack and growth time to match.
The code contains a lot of game logic related things, not too concerned about, the key is to get to a box shape description, need to know up,center,size.
Here the laser is considered a long box, the length of which is the maximum attack distance of the laser.
Finally, since some units have multiple collider, all need to be filtered (i.e. m_filter), the principle is simple:
1, find hit or collider corresponding units (Targetpick.from)
2, check whether the unit in the filter has already existed. exists is not processed, does not exist, continues, and is added to the filter (m_filter.test)
3. Calculation logic for Damage (M_info.attack)
Directional Laser (continuous)
The directed laser attacks the specified unit.
Directional lasers do not require collision detection to detect which collider the laser intersects with, since a directed laser is a continuous attack on a specified unit.
To solve the problem, the laser and the unit of the collision point exactly where. According to this collision point, the shape of the laser is drawn.
The implementation code for its attack:
Public BOOL_takeattack ( outVector3 Point) { Point=Vector3.zero; Vector3 v=utils.up (transform); //The colliders in the array is sorted in order of distance from the origin pointRaycasthit2d[] Hits =Physics2d.raycastall (transform.position, V, m_info.attackdistance, Layermanager.getlayer (m_Faction). oppUn Itmask); //is there a hit for target ?Targetpick pick =Targetpick.none; for(ints =0; S < hits. Length; ++s) {pick= Targetpick.istarget (Target,refhits [s]); if(pick) Break; } if(pick) { point=Pick.point; M_info.attackon (pick, V, M_myunit, Const.none_effect, This); if(Pick.unit && pick.unit.curHp <=0) return false; if(Pick.part && pick.part.unit.curHp <=0) return false; return true; } return false; }
The attack function returns whether to attack the object Unit (target) and returns the attack point. And the difference between penetrating lasers, using raycastall to just find out if the object to be attacked is in it, and determine the point of collision.
Directional Laser (single attack)
Similar to the lightning effect in the diagram. The principle of continuous directional laser is basically the same, the difference is a single attack when playing lightning (or other effects) animation.
As with the continuous directional laser, the raycast is used to judge whether the Lightning "hits" The target in the course of the attack.
If there is no need to immediately interrupt the attack and attack animation, the attack unit in the event of a sudden turn, the lightning will move with the attack unit, there is a bug.
The code is no longer given.
Range attack
Typically, flamethrower or explosions, all units receive damage within a certain range.
I can't find the picture at the moment. Back to fill.
BOOL_takeattack () {Vector3 dir=utils.up (transform); M_filter.clear (); Raycasthit2d[] Hits=Physics2d.circlecastall (transform.position, M_info.attackdistance, Vector2.zero, Ma Thf. Infinity, Layermanager.getlayer (m_faction). Oppunitmask); BOOLf =false; //is there a hit for target ? for(ints =0; S < hits. Length; ++s) {Vector3 v= Utils.v2tov3 (hits [S].point)-transform.position; if(Mathf.abs (Vector3.angle (dir, v)) < M_INFO.ATTACKARC/2) {f=true; Targetpick Pick= Targetpick.from (refhits [s]); //AOE Circlecastall may not be able to select Shieldpick. Toshield (); if(pick)if(M_filter.test (refpick)) M_info.attackon (pick, Vector3.zero,NULL, Const.none_effect, This); } } returnF; }
The principle is very simple, first find all the collider in the Circle range, and then determine whether it is within the fan angle of the spray guns.
Attack, ballistic this piece of content game logic is a game of a focus, in fact, it is difficult to write an article to say that, in fact, I would like to u3d from the bottom of the physics, drawing to the top of the code logical structure of the most clear,
But found that writing a blog time is longer than I think, long until I write a function of the code to achieve the time has not written an article long.
So, I think about it, can't expect all things to be clear, most of the principle, unlike most of the online tutorial is about the most basic content, or even explain the API,
But the establishment of the reader has a certain basis, the principle, speaking structure, speaking from the difficult points, to help themselves comb the game code, but also the key technical points to do a memo.
Next trailer: The graphical effect of the trajectory, the structure and key points of the attack logic (for example, the meaning of the classes defined in the code are shown in this article)
(The end of this article, the next article to be continued)
Uncle Ciba's solo tour-battle! The trajectory realization (middle)