Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by skakac.33 · Dec 01, 2013 at 07:40 PM · 2dphysicsprojectiletrajectoryprediction

2D projectile trajectory prediction

I have problems with predicting trajectory I'm using:

 Vector2 startPos;
 float power = 10.0f;
 float interval = 1/30.0f; 
 GameObject[] ind;
 
 
 void Start (){
         transform.rigidbody2D.isKinematic = true;
         ind = new GameObject[dots];
         for(int i = 0; i<dots; i++){
             GameObject dot = (GameObject)Instantiate(Dot);
             dot.renderer.enabled = false;
             ind[i] = dot;
         }
     }
 
 void Update (){
         if(shot) return;
         if(Input.GetAxis("Fire1") == 1){
             if(!aiming){
                 aiming = true;
                 startPos = Input.mousePosition;
                 ShowPath();
             }
             else{
                 CalculatePath();
             }
             
         }
         else if(aiming && !shot){
             transform.rigidbody2D.isKinematic =  false;    
             transform.rigidbody2D.AddForce(GetForce(Input.mous  ePosition));        
             shot = true;
             aiming = false;
             HidePath();
         }
 }
 
 Vector2 GetForce(Vector3 mouse){
         return (new Vector2(startPos.x, startPos.y)- new Vector2(mouse.x, mouse.y))*power;
         
 }
 
 void CalculatePath(){
         ind[0].transform.position = transform.position; //set frist dot to ball position
         Vector2 vel = GetForce(Input.mousePosition); //get velocity
 
         for(int i = 1; i < dots; i++){            
             ind[i].renderer.enabled = true; //make them visible
             Vector3 point = PathPoint(transform.position, vel, i); //get position of the dot 
             point.z = -1.0f;
             ind[i].transform.position = point;
         } 
     }
 
     Vector2 PathPoint(Vector2 startP, Vector2 startVel, int n){
                //Standard formula for trajectory prediction
         float t = interval;
         Vector2 stepVelocity = t*startVel;
         Vector2 StepGravity = t*t*Physics.gravity;
 
         Vector2 whattoreturn = ((startP + (n * stepVelocity)+(n*n+n)*StepGravity) * 0.5f);
 
         return whattoreturn;
     }



Using this, I get wrong trajectory. 1. It's like gravity doesn't drag trajectory down at all, and yes i know that gravity is weak because:

 t*t*Physics.gravity = 0.03^2 * vector2(0, -9.8) = vector2(0, -0.00882)

But that is the formula :S 2. Since gravity is low, velocity is too strong.

Here is the video: http://tinypic.com/player.php?v=1z50w3m&s=5

Trajectory formula form: http://www.iforce2d.net/b2dtut/projected-trajectory

What should I do?

I found that if I set StepGravity to something stronger like (0, -0.1) and devide startVel by 8 I get nearly right trajectory, but i don't want that, I need true trajectory path.

Comment
Add comment · Show 1
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 Ankit Priyarup · Jan 03, 2014 at 02:35 PM 0
Share

The script is showing errors

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Shadowphax · Jun 30, 2014 at 10:58 AM

I have a non-mathematical solution that I have not tested but I believe it can work.

At regular intervals (while the player is aiming) fire a dummy object with a trail renderer with the same velocity and angle as you would do normally. But on this object just remove the collider. The result should be a pulsing trail that shows the predicted trajectory, even with bounces and so forth.

In this way you can let Unity do the math.

Comment
Add comment · 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
0

Answer by MarkD · Dec 01, 2013 at 09:28 PM

That is a tough one. I think you might even have a better change asking your question on StackOverflow. On that forum is a bigger group of mathematical coders.

Comment
Add comment · 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

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

19 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

Related Questions

2D Trajectory/Path Predictor 0 Answers

Tank AI, 2d rigidbody projectile trajectory prediction 0 Answers

simple trajectory for rigidbody 2 Answers

Aim preview for projectile trajectory 1 Answer

Trajectory Prediction with Particle Systems 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