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
0
Question by byerdelen · Jun 04, 2015 at 11:43 AM · physicsprojectilemotiontrigonometry

Simple projectile motion with wind

alt textHi, I am a programmer myself but trigonometry is not my favourite area if things get complicated. I made a prototype that calculates the launch degrees of a thrown ball with a changing speed that is affected by changing wind(horizontal) and changing gravity(vertical), that the ball going from specific destination (character position)A to (target position)B. Then throws a projectile starting from the same angle of the character and affected by the same speed, wind, gravity. So when the player turns to the first calculated position, the projectile reaches the destination B.

Here is the code for the prototype:

Calculation.cs :

 public class Calculation : MonoBehaviour
 {
 public float speed;
     public float verticleangle;
     public float horizontalangle;
     public float windangle;
     public float gravity;
     public float wind;
     public Transform target;
 public GameObject arrow;
 void Update ()
     {
   // Calculate distance to target
         float target_Distance = Vector3.Distance(transform.position, target.position);
         // Calculate the verticle angle needed
         verticleangle = Mathf.Asin(target_Distance * gravity / speed) / 2 * Mathf.Rad2Deg;
         // Calculate the horizontal angle needed
         windangle = Mathf.Asin(target_Distance * wind / speed) / 2 * Mathf.Rad2Deg;
 
         if (float.IsNaN(verticleangle))
             print("outofrange");
  if (Input.GetKeyDown(KeyCode.Space))
             fire();
 }
   void fire()
     {
         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
         GameObject temparrow = Instantiate(arrow, transform.position, transform.rotation) as GameObject;
         StartCoroutine(temparrow.GetComponent<Throw>().throwaction(speed, verticleangle, windangle, gravity, wind, rotation ));
     }
 }


Throw.cs

 public class Throw : MonoBehaviour
 {
 public  IEnumerator throwaction(float speed, float verticleangle, float windangle, float gravity, float wind, Quaternion orgrot)
     {
         //Extract the X  Y componenent of the velocity
         float Vx = Mathf.Sqrt(speed) * Mathf.Cos(verticleangle * Mathf.Deg2Rad);
         float Vy = Mathf.Sqrt(speed) * Mathf.Sin(verticleangle * Mathf.Deg2Rad);
         float Vz = Mathf.Sqrt(speed) * Mathf.Sin(windangle * Mathf.Deg2Rad);
         Quaternion newrot = transform.rotation;
         float elapse_time = 0;
         Vector3 oldpos;
 
         while (true)
         {
             elapse_time += Time.deltaTime;
             transform.rotation = newrot;
             oldpos = transform.position;
             //Move the projectile
             transform.position += Vx * Time.deltaTime * ( transform.forward);
             transform.position += (  - (gravity * elapse_time)) * Time.deltaTime * (transform.up);
             transform.position += ( - (wind * elapse_time)) * Time.deltaTime * (-transform.right);
             //Rotate the player
             transform.rotation = Quaternion.LookRotation(transform.position - oldpos);
 
             if (elapse_time > 8)
             {
                 Destroy(gameObject);
                 yield break;
             }
 
             yield return null;
         }
     }
 }



Can someone tell me what is going on wrong in this code?

Here is the working prototype as web build to try out:

Web build

Here is my prototype that I am using:

Prototype

Thanks

shape-0000-your-shape-here.png (32.0 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

20 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

Related Questions

A job task : Projectile Motion 0 Answers

Projectile motion with angle and force in one direction 1 Answer

Finding angle of angular motion 1 Answer

Projectile(Arrow) angle during flight is not realistic 1 Answer

bullet realistic penetration 0 Answers


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