Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Aisenhein · Sep 20, 2017 at 07:42 PM · linerenderermathfcalculationprojectiles

Line renderer projectile trajectory in 3D world

Hello,

I'm having a trouble with a script to calculate de ball trajectory of the cannons in my game.

I have a root object, inside him are the cannon mesh and a empty transform called point that I use for position and rotation when instantiate the ball on shoots.

I use a linerender on that point and a control script on the root to calculate the trajectory and shoot the cannon when needed.

My problem is the trajectory calculation, I already successful calculate it and it works fine, but it is a script that only 100% if my game was 2D, but the game is 3D.

So when I rotate the root in the Y axis the line renderer rotates too, but its not in the right location anymore, some codes tests make it off-set from the point's origin and other makes the curvature of the parabola changes too without changing the power of the shoot and not the right forward from point.

The actual code:

         Vector3 vel = point.forward * power;
         //Vector3 vel = point.TransformDirection(new Vector3(0, 0, power));
         Vector3 pVelocity = vel / 1;
 
         float velocity = Mathf.Sqrt((pVelocity.y * pVelocity.y) + (pVelocity.z * pVelocity.z));
         float angle = Mathf.Atan2(pVelocity.y, pVelocity.z);
         float fTime = 0;
 
         lineRenDebug.positionCount = numOfTrajectoryPoints;
 
         for (int i = 0; i < numOfTrajectoryPoints; i++) {
             float dx = velocity * fTime * Mathf.Cos(angle);
             float dy = velocity * fTime * Mathf.Sin(angle) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
 
             Vector3 pos = new Vector3(0, dy, dx);
             //Vector3 pos = new Vector3(transPonta.position.x, dy, dx);
 
             //Vector3 finalPos = Vector3.Scale(point.forward, pos) + point.position;
             //Vector3 finalPos = point.TransformDirection(point.position + pos);
             Vector3 finalPos = point.position + pos;
             lineRenDebug.SetPosition(i, finalPos);
 
             fTime += 0.15f;
 
             if (finalPos.y < -2f) {
                 lineRenDebug.positionCount = i;
                 break;
             }
         }

The comments are some changes that I tried and not worked.

Someone can try to help me? Or a tip that something that I'm missing here.

Thanks.

justrotatedtheroot.png (45.5 kB)
ok.png (49.2 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
1
Best Answer

Answer by Ravengrim · Sep 20, 2017 at 08:13 PM

Those things work a little different in three dimensions. When calculating the magnitude of the velocity vector, you're only taking the y and z coordinates into account, but as soon as you rotate your cannon, you need x as well. Take a look at Vector3.magnitude, which is an already implemented method to get the magnitude of a vector.

Comment
Add comment · Show 3 · 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 Aisenhein · Sep 21, 2017 at 12:36 AM 0
Share

Thanks for the answer man. Did you mean to uses pVelocity.magnitude to assign float velocity? I tested it but not works too. I understand when you talk about the need of take into account the X so. I think that something are missing but don't know what. I imagined that the point.forward would be enough for take into account all axes. Only if I'm transfor$$anonymous$$g the results in wrong way.

avatar image Ravengrim Aisenhein · Sep 21, 2017 at 02:34 AM 0
Share

Yes, that's what I meant. What that method does is to simply return sqrt(x² + y² + z²), which is the magnitude of the vector (x, y, z) in three-dimensional space. That way you don't have to implement that formula yourself all the time. I recommend checking out the Vector3 class, as there are several other useful methods as well. I don't really know what your other code exactly does, but there's probably something missing a third coordinate as well.

This might help you though: https://www.dropbox.com/s/7y6kgq2hkdrujkv/2017-09-21%2004.10.08.jpg?dl=0

It is the formula for the position of a falling object m (without drag) with the initial speed fwd, the gravity g and the time parameter t. In your case, fwd would be pVelocity, and t would be fTime.

avatar image Aisenhein Ravengrim · Sep 21, 2017 at 04:44 PM 0
Share

Ok, thank you, I will try this formula to confirm.

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

69 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

Related Questions

Trouble calculating Arc offset position 1 Answer

How to zoom in line renderer using the camera 1 Answer

How to calculate inner vertices of a line renderer? (math question) 1 Answer

Adjusting an angle of a collider2D to a linerenderer that is user-created 0 Answers

I have questions about ui linerender and linerender. 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