Unity3d Engine: 2D game automatic aiming algorithm implementation

Source: Internet
Author: User

Ext.: http://blog.csdn.net/naitu/article/details/39555373

In many flying shooter games, there are enemies that automatically aim and fire at the player. Here I use the new version of the Unity3d engine 2D system to achieve this function.

First, let's take a look at the theoretical knowledge of the principle. We can put the enemy and the player in a coordinate system, the enemy is the coordinates is the origin ((0,0) point), the player is a point in this coordinate system. The two are then built into right triangle in the coordinate system to calculate how many angles the x axis of the coordinate system rotates toward the player.

Now suppose the player is placed in the four quadrants of the coordinate system, assuming that the first quadrant coordinates are (x, y), assuming that the second quadrant coordinates are (-x,y), assuming the third quadrant coordinates (-X,-Y), assuming that the quadrant coordinates are (x,-y). To find out their angles separately.

Tan angle = (enemy coordinates y-player coordinate x)/(enemy coordinates × player coordinate X.).

To use the code to find the angle, it is necessary to cut the value above. Note that the value after the inverse tangent is the radian value is not the angle value. And we're going to do it in radians. The code is as follows.

Angle =mathf.rad2deg*mathf.atan (TRANSFORM.POSITION.Y-M_PLAYER.POSITION.Y)/(transform.position.x-m_ player.position.x));

When the player is in the first image, the rotation angle is positive. When the player is in the fourth image limit, the rotation angle is negative. Note that the angle of rotation here is the angle at which the enemy coordinates x-axis points to the player's rotation. In unity, the y-axis is generally the upward direction. So we want the y-axis to point to the player and let the enemy rotate 90 degrees clockwise. That is, the angle value minus 90 °.

angle=angle-90;

When the player is in the third image, the rotation angle is positive. When the player is in the second image limit, the rotation angle is negative. Note that the rotation angle here is the enemy coordinate-X axis that points to the angle that the player wants to rotate. We want the y-axis to point to the player and let the enemy rotate 90° counterclockwise. That is, the angle value is added to 90°.

angle=angle+90;

It is easy to determine whether the player is in the 14 quadrant or the two or three quadrant of the coordinate system. Enemy x-coordinate minus player x-coordinate less than 0 o'clock, player in 14 quadrant. Otherwise in the two or three quadrant.

if (transform.position.x-m_player.position.x < 0)

angle=angle-90;

Else

angle=angle+90;

To find out the angle of rotation, let's set the object's own Euler angle. Because we're going to rotate the plane of the XY axis. We are going to rotate with the z axis as the rotation axis. Rotates the angle degree around the z axis of the object around its own coordinate system.

Transform.localeulerangles=new Vector3 (0,0,angle);

This allows the enemy to target automatically based on the player's position. The final code is as follows;

usingUnityengine;usingSystem.Collections; Public classEnemy:monobehaviour {//apply a variable to store the angle value. Private floatangle;//apply a variable to store the player's location.  PublicTransform M_player;//apply a variable to store the prefab of a gun game object.  PublicTransform gun;voidStart () {}voidUpdate () {//Calculation AngleAngle=mathf.rad2deg*mathf.atan (TRANSFORM.POSITION.Y-M_PLAYER.POSITION.Y)/(Transform.position.x-m_player.position.x));//determine the quadrant in which the angle is located and make corrections. if(Transform.position.x-m_player.position.x <0) Angle=angle- -;ElseAngle=angle+ -;//set the object's own Euler angle, is the object around its own coordinate system in the z axis, rotation z degree. Transform.localeulerangles=NewVector3 (0,0, angle);//generate a Gun object. //Instantiate (gun, transform.position, transform.rotation);}}

Unity3d Engine: 2D game automatic aiming algorithm implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.