- Home /

# shoot a cannon ball cannot run !!

Dear everyone . I'm doing game kingdomrush in 2D space (demo by cocos2d-x) i've a cannon shoot shoot bullet like a ball move in a parabol path. i see this link : http://answers.unity3d.com/questions/148399/shooting-a-cannonball.html

with this code in unity :

```
function BallisticVel(target: Transform, angle: float): Vector3 {
var dir = target.position - transform.position; // get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude ; // get horizontal distance
var a = angle * Mathf.Deg2Rad; // convert angle to radians
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle
dist += h / Mathf.Tan(a); // correct for small height differences
// calculate the velocity magnitude
var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return vel * dir.normalized;
}
var myTarget: Transform; // drag the target here
var cannonball: GameObject; // drag the cannonball prefab here
var shootAngle: float = 30; // elevation angle
function Update(){
if (Input.GetKeyDown("b")){ // press b to shoot
var ball: GameObject = Instantiate(cannonball, transform.position, Quaternion.identity);
ball.rigidbody.velocity = BallisticVel(myTarget, shootAngle);
Destroy(ball, 10);
}
}
```

i convert to cocos2d-x :

```
CCPoint Boom :: canculateFiringSolution()
{
CCPoint dir = ccpSub(m_target, m_start); //m_start is start postion of bullet
float h = dir.y;
dir.y = 0;
float dist = ccpLength(dir);
float a = CC_DEGREES_TO_RADIANS(45);
dir.y = (float)dist* tan(a);
dist = dist + h/tan(a);
float vel = sqrt(dist * 0.01/sin(2*a)); // i must set gravity = 0.01 because if i set -1 same code below. i will run wrong
return ccpMult (ccpNormalize(dir),vel);
}
void Boom::update(float dt)
{
m_V = canculateFiringSolution(); //m_v is vector of velocity
m_bullet->setPosition(CCPoint (m_bullet->getPositionX() + m_V.x, m_bullet->getPositionY() + m_V.y ));
}
```

i see i convert exactly. but bullet just move linearly, not in parabol path :( Please help me.i've spent many days for this :( Thank you everyone !!! sorry if i post cocos2d-x code in unity3d forum.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Calculating trajectory of elliptical orbit? 2 Answers

Calculate Height of Trajectory : Unity3d 1 Answer

bullet realistic penetration 0 Answers

Please help, bullet drop is wrong at certain angles ? 0 Answers

Collision impact force 1 Answer