Similar to the flight trajectory of Angry Birds, the flight trajectory of birds
Type of throwing motion:
"Many bullets not only vertically but also follow the horizontal movement. Then they are moving horizontally as they move up or down. Body Movement-horizontal and vertical
The two components of motion.
Vertical Motion:
In vertical motion, gravity acts on the object, and the negative acceleration "-9.8 Mb/s" (center of gravity acceleration) is given ). This means that the object speed is reduced by-9.8 m/² per second. The Free drop speed is V = g * t. If we have an initial velocity, the equation of the falling velocity of an object is: V = Vi + g * t. The acceleration is-9.8 Mbit/s, and the distance calculation equation is used for the free falling body; S = 1/2 * g * t; considering the initial object speed
Formula calculation; S = Vi * t-1/2 * g * t; the distance is subtracted because g is downward.
Horizontal movement:
In horizontal motion, there is no external force acting at a constant speed in the horizontal direction. Therefore, the X component is a constant velocity, and the acceleration in the X direction is zero. The distance and velocity equations are given below. S = v * t; below is a simple c # code that will throw along the path when the ball's ballistic path is displayed.
Note: add the following script to the gun object. Create a prefebs ball and trajectory point to be instantiated at runtime. The ball must have Collider and Rigidbody.
:
using UnityEngine;using System.Collections;using System.Collections.Generic;public class CannonScript : MonoBehaviour {// TrajectoryPoint and Ball will 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 are 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 mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed. 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 two 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 two arguments, start position of object(ball) and initial velocity of 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 * Mathf.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; } }}