A flying trajectory like Angry Birds

Source: Internet
Author: User

??


Type of projectile motion:
"Many bullets not only move vertically but also follow the level of movement. It is also moving horizontally when they move up or down. Motion of the projectile--transverse and longitudinal

Two parts of the movement.
Vertical movement:
In vertical motion, gravity acts on the object and gives a negative acceleration of " -9.8 m/s2" (center of gravity acceleration). This means that the speed of the object decreases by 9.8 m/2 per second. The velocity of the freefall is V = g * t. If we have an initial velocity then, the object falling velocity equation: V = Vi + G * t acceleration is -9.8 m/s2, the equation for calculating the distance when doing free fall; s= * g * t * t; Considering the initial velocity of the object

The formula is calculated; S = Vi * T-1/2 * g * t * t; distance is subtracted because the direction of G is downward.
Transverse motion:
In the horizontal movement, there is no external force in the horizontal direction uniform motion. Thus, on this basis, the X component of the constant velocity, the acceleration in the x direction is zero. The equations for calculating distances and velocities are given below. S = v * t; The following is a simple C # code that will show the trajectory of the ball when it is thrown along the path.
Note: Add the following script to the gun object. Create Prefebs balls and trace points when the runtime is instantiated. The ball must have collider and rigidbody.
:


Using unityengine;using system.collections;using System.collections.generic;public class Cannonscript:monobehaviour    {//Trajectorypoint and ball would be instantiated public gameobject Trajectorypointprefeb;        Public Gameobject BALLPREFB;    Private Gameobject ball;    private bool ispressed, Isballthrown;    Private float power = 25;    private int numoftrajectorypoints = 30;    Private List trajectorypoints;        ---------------------------------------void Start () {trajectorypoints = new List ();        ispressed = Isballthrown =false;//trajectorypoints is instatiated for (int i=0;i<numoftrajectorypoints;i++)            {gameobject dot= (gameobject) instantiate (Trajectorypointprefeb);            dot.renderer.enabled = false;        Trajectorypoints.insert (I,dot);         }}//---------------------------------------void Update () {if (Isballthrown) return;   if (Input.getmousebuttondown (0))     {ispressed = true;        if (!ball) Createball ();            } else if (Input.getmousebuttonup (0)) {ispressed = false;            if (!isballthrown) {throwball (); }}/When the mouse button is pressed, cannon are rotated as per mouse movement and projectile trajectory path is D        Isplayed. if (ispressed) {Vector3 vel = getforcefrom (Ball.transform.position,camera.main.screentoworldpoint (Input.            MousePosition));            float angle = mathf.atan2 (vel.y,vel.x) * MATHF.RAD2DEG;            Transform.eulerangles = new Vector3 (0,0,angle);        Settrajectorypoints (Transform.position, Vel/ball.rigidbody.mass); }}//---------------------------------------//Following method creates new ball//---------------------        ------------------private void Createball () {ball = (Gameobject) instantiate (BALLPREFB); Vector3 pos = transForm.position;        Pos.z=1;        Ball.transform.position = pos; Ball.    SetActive (FALSE); }//---------------------------------------//Following method gives force to the ball//----------------------- ----------------private void Throwball () {ball.            SetActive (TRUE);        Ball.rigidbody.useGravity = true; Ball.rigidbody.AddForce (Getforcefrom (Ball.transform.position,camera.main.screentoworldpoint (        input.mouseposition)), forcemode.impulse);    Isballthrown = true; }//---------------------------------------//Following method returns force by calculating distance between given Points//---------------------------------------private Vector2 getforcefrom (Vector3 frompos, Vector3 ToPos    {return (new Vector2 (Topos.x, Topos.y)-New Vector2 (Frompos.x, frompos.y)) *power; }//---------------------------------------//Following method displays projectile trajectory path. It takes arguments, start position of object (ball) and initial velocity of the object (ball).    ---------------------------------------void Settrajectorypoints (Vector3 pstartposition, Vector3 pvelocity)        {Float velocity = mathf.sqrt ((pvelocity.x * pvelocity.x) + (PVELOCITY.Y * pvelocity.y));        float angle = mathf.rad2deg* (Mathf.atan2 (PVELOCITY.Y, pvelocity.x));                float ftime = 0;        Ftime + = 0.1f; for (int i = 0; i < numoftrajectorypoints; i++) {Float dx = velocity * ftime * mathf.cos (angle * M) ATHF.            Deg2rad);            float dy = velocity * ftime * mathf.sin (angle * mathf.deg2rad)-(Physics2D.gravity.magnitude * ftime * ftime/2.0f);            Vector3 pos = new Vector3 (pstartposition.x + dx, pstartposition.y + dy, 2);            Trajectorypoints[i].transform.position = pos;            Trajectorypoints[i].renderer.enabled = true; Trajectorypoints[i].transform.eulerangles = new Vector3 (0,0,mathf.atan2 (PVELOCITY.Y-(PhySics.gravity.magnitude) *ftime,pvelocity.x) *mathf.rad2deg);        Ftime + = 0.1f; }    }}

A flying trajectory like Angry Birds

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.