Should have been popupmenu, but the specific development of the notebook is not around, the computer on hand is not the latest code,
So you can only pause that for a moment, and talk about another interesting content.--u3d to achieve bullet trajectory.
Of course, this complete said, is also very complex, or simple, first of all, say the core principle.
First, the definition
I divide the bullets into at least two types:
1, solid type. After launch, a gameobject with a rigid body is generated, which can be detected by U3D's physical engine. such as machine gun bullets, ballistics and so on.
2, radial type. That is, the motion and collision of bullets are not implemented by rigid bodies, and trajectory control is implemented by code, which is detected by physics2d functions or by calculating objects themselves. such as directional laser, AOE damage.
This article is about two kinds of realization.
Second, understand the u3d physics engine
Knowledge of the reason, in order to better and faster code implementation. This chapter begins with the use and phenomenon, and illustrates some "pits" about the u3d physics engine.
Collider and Rigidbody
If you do not understand U3d Collider (collision body), rigidbody (rigid body) can first find the relevant articles to see, many good articles on the web.
Here with your own understanding of the simple (for 2D):
Collider depicts the collision shape of the object, with box, cirlce, etc., is the basis of collision detection, such as mouse click Collision judgment, are dependent on the definition of collider to know the object's "collision shape"
Rigidbody is to give an object a property that accepts physical laws, such as a small ball with a rigid body, and the default ball will receive the gravitational acceleration down.
Rigid bodies must have collider, or there will be no "shape"
The design concept of 2D physical effects in u3d is:
An object that does not move, only collider, such as the earth and trees;
Moving, need to have physical motion effect, set collider and Rigidbody,collider description "shape", rigidbody explain physical properties, such as quality, collision properties.
Pay attention to this idea, because I first use 2D physics to the game, there are many places very confusing, do not understand why so design, if understand this idea, a lot of things to understand.
Correlation parameters for Collision detection
U3d's 2D physics engine enables rigid body motion collisions without any coding.
But if you need code to get the collision information, the related properties are:
Collider is Trigger,rigidbody's is kinematic
As for the role of other rigid body parameters, you can set a scene to adjust the parameters to see the effect, the basic understanding.
Because I mainly want to get the collision information through the U3d 2D physics engine, but do not care about the physical effects after the collision, so the other parameters can be set to 0.
In particular, since the game is a Top-down perspective game, and U3d's 2D physics engine default y-axis is the upper and lower direction of the 2D world, the gravity scale is set to 0.
The corresponding related functions are:
Ontriggerenter2d, ONTRIGGEREXIT2D, ontriggerstay2d (called when a collision occurs collider)
Oncollisionenter2d, ONCOLLISIONEXIT2D, oncollisionstay2d (called when a collision occurs Rigidbody)
The effects of these properties and functions are explained below.
the recommended implementation mode of collision detection--rigidbody collision
This setting is required when collision detection is required with the U3d 2d physics engine (assuming a collision between A and B is detected):
1, A and B are added collider, do not tick is Trigger
2, A or B at least one add rigidbody, do not tick iskenematic
3, add a script to a or B, add oncollisionenter2d, oncollisionexit2d, or oncollisionstay2d function to get the collision information.
Take the solid bullet of this article as an example:
1, to game units and bullets are added collider
2. Add Rigidbody to Bullets
3. Add the Oncollisionenter2d method to the bullet, write the logic code causing the damage, and destroy the bullet object.
About the properties of the is kinematic for rigidbody: When checked, the 2D physics engine does not work on this rigid body, only the code to achieve the motion of the object. At the same time, oncollisionenter2d will not be triggered.
Another realization of collision detection--collider trigger
"Collision detection" can also be achieved through Collider's is trigger.
Collider is Trigger: As the name implies, this property indicates whether the trigger, after the tick, there will be "collision" ontriggerenter2d, ontriggerexit2d, ontriggerstay2d function.
For example, detecting collisions between A and B
1, A and B are added collider,a check is trigger,b unchecked
2, a add rigidbody
3. Add ontriggerenter2d to a script
Collider of A and B are contacted, then A's ontriggerenter2d is called. If B's script also has ontriggerenter2d, it will be called, although B does not check is Trigger.
This "collision detection", relying on the trigger mechanism of collider, can be done at the collider level, and its principle should be similar to the trigger of mouse click event. However, the following questions are available:
1, this trigger mechanism collision detection frequency and the same as update, and the above recommended method (using ONCOLLISIONENTER2D) is the same as Fixedupdate, the latter is specifically to do rigid body physics operations, its calculation frequency is better, collision detection more accurate. If ontriggerenter2d is used, it is detected that two colliding objects may have been embedded in each other for a long time, and if one of the objects moves too fast, it may have been "worn".
2, oncollisionenter2d parameters provide more information about the collision, and ontriggerenter2d only a collision collider information, not get more accurate points.
3, although the collision is completed at the collider level, feeling with Rigidbody does not have any relationship (1, 22 points of imagination also side confirmed my idea), but A and B must have a rigidbody, otherwise the collision event can not trigger. This is also true for functions such as istouching in Physics2d.
4, after setting trigger, all the collision events are trigger intercepted, ONCOLLISIONENTER2D will not be called again.
Based on the above factors, this collision detection, can not be called as an effective "collision detection", in the actual application to determine whether it is appropriate according to the actual situation.
As a collision detection of objects and objects, it is not recommended to use the Collider trigger method.
A summary of collision detection
1, if you want to use the physical engine to achieve collisions, including collider Trigger,rigidbody Collision,physics 2D istouching and other methods, in addition to the collision of both sides have collider, There must be 1 of them with Rigidbody. (This point makes me unable to spit out the groove).
2, using collider Trigger (tick is Trigger), you can use Ontriggerenter2d, ONTRIGGEREXIT2D, ontriggerstay2d monitoring collision, but no impact physical effects, Rigidbody's collision cannot be used. Collider's trigger does not work at the physical engine level, whether it is the update frequency of the collision detection, the collision result is not good, and it directly "blocks" the physical engine of the effect on the object.
3, using Rigidbody collision (not checked is kinematic), using oncollisionenter2d, ONCOLLISIONEXIT2D, oncollisionstay2d monitoring collision, The physical engine affects the motion of the rigid body, and there is a physical effect of the collision bounce. It is best to only get the state in the oncollisionenter2d without updating the object motion, because the physics engine is also in control of its motion.
In conclusion, there are many limitations on the use of u3d physics engines, and there are many logical functions that can be implemented. Due to the realization of the solid bullet, bullet strike unit after the bullet self-destruction, and above the 3rd just meet, can use u3d collision, and not physical bullets obviously can not be used.
Solid type Bullets
If you read the above analysis and understand the principle, should be the u3d 2D collision (3D similar) of the routine should be very clear, to achieve the solid bullet strike effect, is only a point, matching things.
The Realistic way is:
1, to game units and bullets are added collider
2. Add Rigidbody to Bullets
3. Add the Oncollisionenter2d method to the bullet, write the logic code causing the damage, and destroy the bullet object.
Key code:
voidUpdate () {if(Common.pause)return; M_anim.onupdate (Getcomponent<SpriteRenderer> ()); floatL = time.deltatime *M_info.speed; Transform.position+ = M_direction *l; M_leftdistance-=l; if(M_leftdistance <0||m_destroyself) {Flyermanager.free (Gameobject); } } Public Virtual voidoncollisionenter2d (collision2d coll) {if(m_destroyself)return; Targetpick Pick=Targetpick.from (coll); M_info.attackon (Pick,m_direction, M_myunit,hiteffecttype); M_destroyself=true; }
Many of these classes and functions have been encapsulated, such as
Flyermanager.free (), the internal realization of the bullet recovery, easy to reuse.
Again, such as Targetpick and Attackon, the realization of the most appropriate game units to pick up and calculate the ability to combat damage.
adjourned
Uncle Ciba's solo tour-battle! Trajectory realization (UP)