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
0
Question by underdog1515 · Sep 10, 2014 at 10:48 AM · lerptrajectoryprojectilessmoothly

Smooth object moving. Lerp issue

Hello. Sorry for my bad English. I'm making 2d game and I need to realize projectile motion in it. I wrote this code to make it work:

     private float angle;
     private float velocity;
     private float g = 9.80665f;
     private float t;
     private float flightTime;
     
     void Start()
     {
         // scale object
         var scale = CameraSetter.YMax * 2 / 4;
         transform.localScale = new Vector3(scale, scale, 0);
 
         //set desirable parameters for projectile motion
         t = 0;
         var maxHeight = CameraSetter.YMax + CameraSetter.YMax/2;
         var range = CameraSetter.XMax*2;
         angle = CalculateAngle(maxHeight, range);
         velocity = CalculateVelocity(range);
         flightTime = CalculateFlightTime();
     }
     
     private float CalculateFlightTime()
     {
         return (2*velocity*Mathf.Sin(angle*Mathf.Deg2Rad))/g;
     }
 
     private float CalculateVelocity(float range)
     {
         return Mathf.Sqrt((range * g) / Mathf.Sin(angle * 2 * Mathf.Deg2Rad));
     }
 
     private float CalculateAngle(float maxHeight, float range)
     {
         return Mathf.Atan(4*maxHeight/range)*Mathf.Rad2Deg;
     }
     
     void Update()
     {
         t += Time.deltaTime;
         var x = GetXv();
         var y = GetYv();
         transform.position = new Vector3(x, y, 0);
     }
 
     private float GetYv()
     {
         return ((velocity * t * Mathf.Sin(angle * Mathf.Deg2Rad)) - 0.5f * (g * (t * t))) - CameraSetter.YMax;
     }
 
     private float GetXv()
     {
         return (velocity * t * Mathf.Cos(angle * Mathf.Deg2Rad)) - CameraSetter.XMax;
     }

This works fine to me: object starts moving from left bottom corner to right bottom corner with parabolic trajectory and maximum height in center (with coordinates x=0, y=orthographicSize/2). However, it's moving not smoothly, so i decided to do Lerp like this:

 transform.position = Vector3.Lerp(transform.position, new Vector3(x, y, 0), Time.deltaTime);

instead of

 transform.position = new Vector3(x, y, 0);

and this actually ruined the whole thing!! Trajectory now ends in somewhere near the center bottom with max height just about y=-orthographicSize/3 or something like that. Please explain me why Lerp is ruining trajectory and how can i fix it (i need working trajectory with smooth movement).

Comment
Add comment · Show 6
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 RoCkHeArTeD · Oct 06, 2015 at 01:19 PM 0
Share

Can you please tell me what is CameraSetter's purpose?

avatar image underdog1515 RoCkHeArTeD · Oct 06, 2015 at 01:32 PM 0
Share

CameraSetter is singleton object which calculates in Awake() distance from center of screen to edge of screen on both axes based on aspect ratio and ortho size. Y$$anonymous$$ax is ortho size and X$$anonymous$$ax is (ortho size multiply aspect). Therefore distance from left edge to right edge is (2 multiply X$$anonymous$$ax) and distance from top to bottom is (2 multiply Y$$anonymous$$ax).

avatar image RoCkHeArTeD underdog1515 · Oct 06, 2015 at 02:29 PM 0
Share

Can you please post its code here?

Show more comments

1 Reply

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

Answer by khenkel · Sep 10, 2014 at 11:23 AM

That's because you're using Time.deltaTime as the fraction parameter in Lerp(). This is NOT an actual time value but the time in seconds it took to complete the last frame.

I didn't read up intensively on your code, but you could use your "t" variable instead as an example. In this case the object would move to the target position in exactly 1 second.

EDIT:

Try this out:

 private const float MOVEMENT_TIME = 5.0f; // Should move for 5 seconds
 
 void Update()
 {
     t += Time.deltaTime;
     float lerpFraction = t / MOVEMENT_TIME;
     var x = GetXv();
     var y = GetYv();
     transform.position = Vector3.Lerp(transform.position, new Vector3(x, y, 0), lerpFraction);
 }

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 underdog1515 · Sep 10, 2014 at 02:26 PM 0
Share

Thanks! It works perfect now! I'm sorry I cannot vote yet.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Annoying smooth rotation jitter bug 1 Answer

transform.position vector3.lerp appears to stay still for a while before the 5DP matches the new location 1 Answer

Why is my Mathf.Lerp not lasting for the intended duration, even though I'm not using Time.time as the t variable? 1 Answer

Move A Camera Between Two Points Smoothly 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