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
1
Question by Hamesh81 · Apr 11, 2015 at 01:28 PM · positionupdatelerpstart

Saving the starting position of a Lerp during Update

I have seen a number of examples for Vector3.Lerp where the original starting position is stored in the start function and then later used to compare the current position in relation to the end target position. My issue is that I need to store the starting position of the lerp in update not start, since the lerp will be happening on a gameobject being moved around the scene and therefore its starting position will need to be set each time manually. Here is my simple code which sits in update:

     if (doLerp) {
                     startPos = transform.position;
                     transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * speed);
     }

The "doLerp" is set to true whenever a lerp needs to take place and hence when a "startPos" needs to be stored. The issue with the above of course is that once the "startPos" is stored it will continue to be updated while "doLerp" is true, and since the lerp is changing the transform.position this will mean that the startPos will change as well instead of staying the same. How can I record the starting position of a transform before a lerp and not have it changed when the lerp begins?

I need this so that I can do calculations such as the distance between the starting and target positions, and then compare those to the distance between the current and target positions; and find out the percentage of the "trip" that has been completed.

Comment
Add comment · Show 4
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 Baste · Apr 11, 2015 at 01:57 PM 0
Share

Can't you set the startPos wherever you set doLerp to true?

ie:

 if(someCondition && !doLerp) {
     doLerp = true;
     startPos = transform.position;
 }
avatar image Pharaoh_ · Apr 11, 2015 at 01:59 PM 0
Share

I don't fully comprehend the issue, merely because I don't get why you want to make those calculations. You're explaining the technical reasons, but you're not giving us a context. Do you continuously use the Lerp? When is doLerp set to false? If one procedural movement finishes, another one commences right away? Honestly, I would use an IEnumerator for this. Before the while loop, I would have the value stored in a variable and upon exiting the loop, I would make my calculations. Inside of the loop, it would set the transform.position appropriately, without messing with the previous variable.

avatar image Hamesh81 · Apr 12, 2015 at 08:38 AM 0
Share

The basic idea of this question, is how to "do something" within the update function only once. In this case I have a lerp in update and I would like to store the starting position of the transform before the lerp begins moving the transform. But I need to store this so that the position doesn't continue to update during the lerp. The lerping begins whenever a specific bool is set to true, and therefore will happen several times within a session. Hence why I cannot use Start or Awake.

avatar image Deadcow_ · Apr 12, 2015 at 08:49 AM 0
Share

That is exactly what coroutines do.

2 Replies

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

Answer by Deadcow_ · Apr 11, 2015 at 01:59 PM

You need to store initial position outside the Update function.

But in your case it'll be the best to use coroutine:

 IEnumerator MyLerp()
 {
     if (!doLerp) yield break;
     doLerp = false;
 
     Vector3 initPos = transform.position;
     float elapsed = 0;
     while (elapsed < speed)
     {
         elapsed += Time.deltaTime;
         transform.position = Vector3.Lerp(initPosition, target.Position, elapsed / speed);
         yield return null;
     }
 }

and in update (or better where you set startPos to true)

 if (doLerp) {
     StartCoroutine(MyLerp());
 }

Should work fine

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 Hamesh81 · Apr 12, 2015 at 08:09 AM 0
Share

Thanks for that. This does seem to store the "initPos" only once but for some reason placing the lerp inside the coroutine is making the lerp very choppy. Could this be done by leaving the lerp inside update and only use the coroutine to store the initial position?

avatar image Deadcow_ · Apr 12, 2015 at 08:46 AM 1
Share

In this case code inside While block will be executed as frequent as Update function. $$anonymous$$y code is assumes that 'doLerp = true' is assigned once when needed and won't be assigned again while lerp goes. In your case doLerp probably assigned several times, and coroutine starts several times (which make it choppy)

Okay, there is way to fix this, as I said - you need to put this StartCoroutine where you set startPos to true.

 IEnumerator $$anonymous$$yLerp()
  {
      if (doLerp) yield break;
      doLerp = true;
  
      Vector3 initPos = transform.position;
      float elapsed = 0;
      while (elapsed < speed)
      {
          elapsed += Time.deltaTime;
          transform.position = Vector3.Lerp(initPosition, target.Position, elapsed / speed);
          yield return null;
      }
      doLerp = false;
  }

so, you alter doLerp only inside the coroutine, to ensure it'll be executed once in a time.

don't know where you assign doLerp to true, it may be something like this:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
     doLerp = true;

in update or anything else, just put

 StartCoroutine($$anonymous$$yLerp());

ins$$anonymous$$d of

 doLerp = true;
avatar image Hamesh81 · Apr 12, 2015 at 08:50 AM 0
Share

Thanks for that Deadcow_, I will try this out!

avatar image
1

Answer by jtok4j · Apr 11, 2015 at 02:02 PM

Hey, I'm not much of a programmer, but on my third game, so I thought I'd comment.

Essential, I believe, your solution is already spelled out in your question/description.

You need to add another variable or perhaps two to the scripting. You need a "starting" variable, which is the very beginning one and doesn't change. Then you need a "current" variable (which you've got already, showing the current position when the dolerp is made true. Lastly, you need the "target position " variable, which might or might not change, according to your game.

So, I believe that your choice lies essentially in where to record/set the initial/starting position variable. Do you want to set it as soon as the object is created/spawned? Then use void Awake () { to get that right at the beginning. Otherwise, you may prefer to use a separate script to record this, if you must set the initial location later, perhaps with a collider of some sort, if this object is moving. (Set the collider around the object, so when it starts moving, it fires the script which record the colliding object's locations.

Sorry for the generalizations. Just trying to help that creative spark continue onward!

Comment
Add comment · Show 2 · 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 Hamesh81 · Apr 12, 2015 at 08:47 AM 0
Share

I think you're onto something here, I could store the position only when the lerp is not taking place and that way during any lerp I would be able to use the last position stored before the lerp.

avatar image jtok4j · Apr 12, 2015 at 09:46 AM 0
Share

Yes, Good point: "only when the lerp is not taking place". :) Now, just add this to what Deadcow_ has shown you and the pieces will come together. $$anonymous$$eep us updated, and we'll try to continue to help out. :)

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

22 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

Related Questions

Script variables resetting on play 0 Answers

Vector3.Lerp works outside of Update() 3 Answers

Ping pong position using lerp 3 Answers

How do I translate around a circle? 3 Answers

Move object position on specific axis with Lerp 0 Answers


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