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
3
Question by ninjaboynaru · Jan 01, 2014 at 01:15 AM · 2dphysicsmathtrajectory

Drawing projectile trajectory

I have been trying to draw the trajectory path of a launched object using a line render but the line never seems to match the actually path the object takes.

I've already spent hours watching physics tutorials and scouring the web for an answer, but nothing seems to work.

This is a 2D game, so all objects are 2D(if that matters). Also I use seconds for the time but instead fixedTimeStep.

Here is pic of the situation.

alt text

Here is code

     var launchPower : float = 20;
     function Update() {
     
     if(Input.GetKeyDown(KeyCode.Space)){
     var projectile : GameObject = Instantiate(prefab1,transform.position,transform.rotation);
     projectile.rigidbody2D.velocity.x = launchPower;
     }
     
     DrawTraject(launchPower);
     
     }
     
     
     function DrawTraject(var velocity : float;){
     
     var line = this.gameObject.GetComponent(LineRenderer);
     line.SetVertexCount(20);
     
     for(var i = 0; i < 20; i++){
     //I multiply by Physics.fixedDelta time so it can units/FixedtimeStep
     line.SetPosition(i, Vector3(transform.position.x + (launchPower * Physics.fixedDeltaTime) * i, transform.position.y - (Physics.gravity.y * Physics.fixedDeltaTime) * (i*i) /2, 0);
     
     }
     
     }
     
     
     
     


trajecotry help.png (23.6 kB)
Comment
Add comment · Show 2
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 Spinnernicholas · Jan 01, 2014 at 01:37 AM 0
Share

That's a hard one. I'll look into it.

avatar image robhuhn · Jan 02, 2014 at 09:15 AM 0
Share

you also might want to vote for a physics simulation/trajectory prediction feature request here: http://feedback.unity3d.com/suggestions/function-for-manually-simulating

5 Replies

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

Answer by HappyMoo · Jan 01, 2014 at 02:59 AM

Ok... let's recreate the physics formula, however, if it turns out to be wrong off too much, it may be a better idea to move the projectile yourself.

Also, I assume for now that you have drag=0 - if not, we have to experiment a bit as drag can be inserted at different stages in the process...

I usually write C#, so excuse me if I produce syntax error on the way - I won't test the code:

 DrawTraject(projectile.transform.position, projectile.rigidbody2D.velocity);
 
 function DrawTraject(var startPos : Vector2, var startVelocity : Vector2;){
 
     var verts = 20;
     var line = this.gameObject.GetComponent(LineRenderer);
     line.SetVertexCount(verts);
 
     var pos:Vector2 = startPos;
     var vel:Vector2 = startVelocity;
     var grav:Vector2 = Vector2(Physics.gravity.x, Physics.gravity.y);
     for(var i = 0; i < verts; i++)
     {
         line.SetPosition(i, Vector3(pos.x, pos.y, 0));
         vel = vel + grav * Physics.fixedDeltaTime;
         pos = pos + vel * Physics.fixedDeltaTime;
     }
 
 }
Comment
Add comment · Show 6 · 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 robertbu · Jan 01, 2014 at 05:43 AM 0
Share

This looks like it will work. I just tested the following code which is very similar, and it tracked the projectile accurately:

 pos = transform.position;
 
 for (var i = 0; i < count; i++) {
     lr.SetPosition(i, pos);
     velocity += Physics2D.gravity * Time.fixedDeltaTime;
     pos += velocity * Time.fixedDeltaTime;  
 }

'velocity' is the initial velocity of the projectile. This calculation assumes no drag.

avatar image HappyMoo · Jan 01, 2014 at 05:47 AM 0
Share

yes, that's the exact same code, cool.

avatar image ninjaboynaru · Jan 01, 2014 at 06:23 PM 0
Share

I think I found the problem. I replaced the projectiles Rigidbody2D with a regular Rigidbody and it worked PERFECTLY!

avatar image HappyMoo · Jan 01, 2014 at 06:51 PM 0
Share

Which code does? You mean the physics is different for 3D and 2D?

avatar image robertbu · Jan 01, 2014 at 07:13 PM 0
Share

Note I tested the code I posted (which is the same code that Happy$$anonymous$$oo posted). It worked perfectly in 2D. Take a look at any settings (especially the drag setting) you had in your Rigidbody2D.

Show more comments
avatar image
2

Answer by idunlop_oefun · Jan 01, 2014 at 02:10 AM

It's difficult to tell from your example code but have you tried turning on the following property:

 line.useWorldSpace = true;

Without that property enabled the line is rendered relative to the game objects position.

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
2

Answer by Spinnernicholas · Jan 01, 2014 at 02:33 AM

You have to square time as well as i and it's easier to use vectors for the math.

 float t = Time.fixedDeltaTime;
 Vector3 Pi = transform.position + new Vector3(launchPower,0,0)*t*i + new Vector3(0,-Physics.gravity,0)*.5*t*t*i*i;
 Pi.z = desiredZ;
 line.SetPosition(i, Pi);
Comment
Add comment · Show 6 · 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 Spinnernicholas · Jan 01, 2014 at 02:36 AM 0
Share

You could also define the velocity and acceleration vectors beforehand to make it really easy to read:

 float t = Time.fixedDeltaTime;

 V = new Vector3(launchPower,0,0);
 A = new Vector3(0,-Physics.gravity,0);

 Vector3 Pi = transform.position + V*t*i + .5*A*t*t*i*i;
 Pi.z = desiredZ;

 line.SetPosition(i, Pi);
avatar image ninjaboynaru · Jan 01, 2014 at 03:02 AM 0
Share

Your solution got me close but it is still off. I;m trying to figure out what it is but I'm no physiyst Here is a pic alt text

trajectory help.png (18.0 kB)
avatar image HappyMoo · Jan 01, 2014 at 03:57 AM 0
Share

Indeed :D

avatar image HappyMoo · Jan 01, 2014 at 04:00 AM 0
Share

No, wait.. that was too fast...We're using Physics.fixedDeltaTime, so we are doing everything that FixedUpdate would do... doesn't matter where the code runs.

I assume Physics.fixedDeltaTime == Time.fixedDeltaTime - I'm new to unity...

avatar image Spinnernicholas · Jan 01, 2014 at 04:16 AM 0
Share

It might be because there is drag. Check the physics settings to see if there could be any other forces.

Show more comments
avatar image
0

Answer by Ankit Priyarup · Jan 10, 2014 at 10:53 AM

http://youtu.be/ZCDiTxBT1nE This is the script created by me i hope this will be helpfull

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 thaitd1985 · Mar 06, 2016 at 08:05 PM

Hi @ninjaboynaru, Do you resolve this issue. I got same issue like you. I replaced with rigidbody2d by rigidbody and it worked perfectly. I don't know what is wrong with rigidbody2d

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

24 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

Related Questions

My character moves in seemingly random directions. 1 Answer

How does the velocity of a ball flying throw a portal change when the portal is rotated? 2 Answers

Simulating a trajectory that takes collisions into account 2 Answers

How to calculate the angle of a trajectory to hit the target 1 Answer

Bomb Trajectory for a "Water Canon" (Maths) 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