Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Reaper93 · May 17, 2017 at 12:58 PM · velocityprojectiletargetsimulationformula

Calculate initial velocity given distance, gravity and angle for a projectile to hit the target at uneven ground

Hello guys!
What i am looking for is a formula to calculate vertical and horizontal (or just the initial) speed for projectile to hit a target which is at a height different than launcher.
I have been looking for the answer for quite a long time and actually found solution for the same height. alt text
Here is the code sample for that:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PArticleProjectile : MonoBehaviour {
     [Range(10,45)]
     public int Angle = 45;
     public float gravity = 9.8F;
     public Transform Target;
 
     public ParticleSystem.VelocityOverLifetimeModule PartS;
 
     void Start ()
     {
         PartS = gameObject.GetComponent<ParticleSystem>().velocityOverLifetime;
     }
     void Update ()
     {
         Vector3 displacement = (transform.position - Target.position);
         float Vx = Mathf.Sqrt((distance * gravity) / Mathf.Sin(2 * Angle * Mathf.Deg2Rad)) * Mathf.Cos(Angle * Mathf.Deg2Rad);
         float Vy = Mathf.Sqrt((distance * gravity) / Mathf.Sin(2 * Angle * Mathf.Deg2Rad)) * Mathf.Sin(Angle * Mathf.Deg2Rad);
         PartS.x = Vx;/*SolveBallisticEquation(displacement.x, displacement.y, gravity, Angle).x;*/
         PartS.y = Vy;/*SolveBallisticEquation(displacement.x, displacement.y, gravity, Angle).y;*/
     }
 }

It works perfectly when target and launcher are on the same height but it starts to deviate when i am changing altitude of either one.
By the way i want to do this via particle system(Velocity over lifetime).When i enable gravity on particles velocity over lifetime starts to behave like initial velocity.
So if anyone has some useful formulas for that i'd appreciate if you share them down in the answers.

0height.gif (184.7 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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Reaper93 · May 24, 2017 at 03:27 PM

Well i ended up solving it by myself with the help of the formula described in this forum Here is my code sample. I hope it will help someone who is is looking for it.It has been really a pain))

  Vector2 SolveBallisticEquation(float distanceX,float distanceY,float gravity,float angle)
     {
         distanceX = Mathf.Abs(distanceX);
         float TotalSpeed;
         Vector2 Solution;
         TotalSpeed = (1/ Mathf.Cos(angle * Mathf.Deg2Rad))*Mathf.Sqrt(0.5F*distanceX*distanceX* gravity /(distanceY + Mathf.Tan(angle * Mathf.Deg2Rad) * distanceX));
         Solution.x = TotalSpeed * Mathf.Cos(angle * Mathf.Deg2Rad);
         Solution.y = TotalSpeed * Mathf.Sin(angle * Mathf.Deg2Rad);
         print(Solution);
         return Solution;
     }

Solution.x is for velocity X ans Solution.y is for velocity Y

Comment
Add comment · Show 1 · Share
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
avatar image ludovicb1239 · Jan 23 at 01:25 PM 0
Share

thanks mate

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculating projectile lead - improving accuracy 0 Answers

My projectile isn't getting fired with any velocity 2 Answers

in projectiles, how to equate horizontal speed as function of Horizontal displacement(how far I want the object to travel sideways) 0 Answers

Projectile velocity correction to always hit stationary spot 0 Answers

Target lead (aim ahead) problem 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