• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by NicolasLIATTI · Jun 16, 2013 at 01:35 AM · physicstrajectory

Drawn trajectory different than real trajectory

Hi all,

I'm trying to draw the trajectory of a projectile (aka Angry bird). I managed to do it, however once I launch the projectile it doesn't exactly follow the trajectory, and I don't manage to find from where comes the difference...

Here is the code I'm using:

 void OnDrawGizmos(){
         GameObject player = GameObject.FindWithTag("Player");
         
         float h = 0.0f;
         
         while(player.transform.TransformPoint(PlotTrajectoryAtTime(h)).y >= 0){
         Gizmos.DrawSphere(player.transform.TransformPoint(PlotTrajectoryAtTime(h)), 0.2f);
         h = h + 0.1f;
         }
             
     }
 
 
 public Vector3 PlotTrajectoryAtTime (float t) {
         return  startVelocity*t + Physics.gravity*t*t*0.5f + startPoint;
 }
 
 
 public void Update(){
 if (Input.GetKeyUp(KeyCode.L)){
         GameObject player = GameObject.FindWithTag("Player");
         GameObject.Find ("Rock").transform.position = player.transform.TransformPoint(0,startPoint.y,startPoint.z); //the object to launch
         
         Vector3 force = player.transform.TransformDirection(startVelocity); // to launch the rock in the good direction
             
         GameObject.Find("Rock").transform.rigidbody.velocity = force;
     }
 }


However the trajectory drawn in the Editor is "higher" than the real trajectory followed by the rock... amy idea?

Cheers,

Nicolas

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jun 16, 2013 at 04:31 PM

Game physics and real world physics are different. Game physics just estimates curved flight using straight line movements over 50 frames/sec. Each line will be a tiny bit over or under the actual curved line, but very close. Unity apparently applies gravity first.

You could move the thrown rock using the real physics formula, then "release" it as a rigidbody when a spherecast or something detected an imminent crash. Or, I think you could tweak the real calculation for your curve to match Unity's. Maybe subtracting 1/2 a frame's worth of gravity from the starting amount might give consistent results over various arcs.

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 NicolasLIATTI · Jun 17, 2013 at 02:02 AM 0
Share

Thanks Owen. Actually, the problem came from the fact that the Player object localSize was different than 1, thus when I used transformDirection to calculate the vector it led to problems. I'm now using an object with a size of 1/1/1 and it works perfectly :)

avatar image Owen-Reynolds · Jun 17, 2013 at 05:12 AM 0
Share

You mean TransformPoint, in line 23. It may have been putting your rock a bit too low. TransformDirection doesn't use scale (since it's often used exactly as you're doing -- to make a speed. TransPoint often places a child, so would care about scale. Both could have been written either way,)

avatar image NicolasLIATTI · Jun 17, 2013 at 05:53 AM 0
Share

It's good to know that TransformDirection doesn't take the scale into account. I will try to replace TransformPoint then, as I'd like to be able to change the size of the player without affecting the trajectory. But when it's set to 1 it works perfectly, it's good :)

avatar image
0

Answer by sufertashu · Nov 03, 2016 at 11:08 AM

I'm having the same problem with the calculated trajectory "higher" than actual trajectory. The difference is consistently about 7% of the "jump" height. I checked the formulas many times and tried various implementations always with the same result. It's not the scale and there is no air drag. Artificially changing the initial velocity and/or direction even by a tiny bit only makes things worse.

If I cannot find a fix I will have to implement my own physics replacement as my game relies on EXACT trajectory, as the trajectory is calculated by the game and cannot be influenced by the player in my case.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

how to predict physics trajectory of a sphere 2 Answers

Show trajectory path a ball will take based on collision 1 Answer

,How can I create trajectory line in my arrow shooting game that use addForce() function? 0 Answers

How to get parabolic trajectory when velocity, height and pitching spot is know ??? 1 Answer

realistic bullet trajectory 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