Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by heavenhell · Oct 16, 2013 at 08:37 PM · physicsmathprojectile

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculating trajectory of elliptical orbit? 2 Answers

Calculate Height of Trajectory : Unity3d 1 Answer

Collision impact force 1 Answer

Shooting a cannonball. 6 Answers

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges