• 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
0
Question by unity_0979C3629A29CFCD264B · Mar 07 at 10:12 PM · uiprojectilerangeheightflight

Projectile shooting with range, height & flight time to display in UI

alt text

I am creating this game by using Leap motion controller, my right hand is Cannon, it shoots cannonballs when the gesture is recognized. Speed of the ball is cannon shots speed & Force on impact is the force calculated on impact which is all working fine but when I am trying to get the correct values of projectile motion such as Range, Height of projectile and Flight duration my logic seems to vanish. I am pretty sure my logic is wrong. I am a beginner in game development. Below is code I am using:

 public float gravity = 9.98f;
     public Transform firingCannon;
     public Text flightTime;
     public Text projectileSpeed;
     public Text projectileDistance;
     public Text RangeofProjectile;
 
     public Transform MyPosition;
     private Transform myTransform;
     public Transform Target;
 
 void Start()
     {
         StartCoroutine(CalcSpeed());
         StartCoroutine(CalcFlightTime());
     }
 
 private IEnumerator CalcFlightTime()
     {
         bool isPlaying = true;
         while (isPlaying)
         {
             Vector3 prevPos = transform.position;
             yield return new WaitForFixedUpdate();
 
             float ProjectileVelocity = Mathf.RoundToInt((Vector3.Distance(transform.position, prevPos) / Time.fixedDeltaTime) / (Mathf.Sin(2 * firingCannon.eulerAngles.z * Mathf.Deg2Rad) / gravity));
 
             projectileSpeed.text = "Projectile Range: " + ProjectileVelocity + " m";
 
             float Vx = Mathf.Sqrt(ProjectileVelocity) * Mathf.Cos(firingCannon.eulerAngles.z * Mathf.Deg2Rad);
             float Vy = Mathf.Sqrt(ProjectileVelocity) * Mathf.Sin(firingCannon.eulerAngles.z * Mathf.Deg2Rad);
             //projectileSpeed.text = "Velocity of Projectile: " + ProjectileVelocity + " m/s";
 
             float velocity = Mathf.Sqrt(Vx * Vx + Vy * Vy);
             // Calculate flight time.
             float flightDuration = Vector3.Distance(transform.position, prevPos) / Vx;
 
 
             projectileDistance.text = "Projectile Duration: " + Vx + " m";
 
             //float elapse_time = 0;
             float TotalflightTime = 2 * Vy / gravity;
             flightTime.text = "Total Flight Duration: " + Vy + " s";
 
             float Range = velocity * Mathf.Sin(firingCannon.eulerAngles.z * firingCannon.eulerAngles.z);
             RangeofProjectile.text = "Range: " + Range + " m";
 }

 
unityanswers.jpg (309.1 kB)
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

229 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to calculate projectile (baseball) distance with drag factor 1 Answer

How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 Answers

Returning Force/Direction to zero 0 Answers

I shoot a projectile, but its rotation is wrong (c#) 1 Answer

De/activating Gameobject depending on distance from different Activators 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