Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 PlasticFrames · Jun 20, 2021 at 09:27 AM · coroutineforeacharray of gameobjectsdashvector3.lerp

Vector3.Lerp curving along three destinations

Hi, I'm working on a game in which the player can freeze time and place up to three destinations (stored in array) and then dash through them in sequence. I've got dash working but it seems to be curving through the from the starting position to the last destination rather than going from point to point. Any help would be appreciated!

 void SetDestinations()
 {
     if (dashNumber < 3)
     {
         dashMarks[dashNumber].transform.position = dashDestination;
         dashMarks[dashNumber].SetActive(true);
         dashOrigin = dashMarks[dashNumber].transform.position;
         dashNumber++;
     }
 }
 
 void TriggerDashes()
 {
     dashNumber = 0;

     foreach (GameObject gameObject in dashMarks)
     {
         if (dashMarks[dashNumber].transform.position != Vector3.zero)
         {
             StartCoroutine(LerpDash(dashMarks[dashNumber].transform.position, dashSpeed));
             dashMarks[dashNumber].SetActive(false);
             dashMarks[dashNumber].transform.position = Vector3.zero; //reset position to limit next dashes
             dashNumber++;
         }
     }
 }

 IEnumerator LerpDash(Vector3 targetPosition, float duration) //still seems to be smoothing between points?
 {
     float time = 0;

     while (time < duration)
     {
         transform.LookAt(targetPosition);
         transform.position = Vector3.Lerp(transform.position, targetPosition, time / duration);
         time += Time.deltaTime;
         yield return null;
     }
     transform.position = targetPosition; //snapping
 }
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 Namey5 · Jun 21, 2021 at 01:18 AM

The problem lies in that you are updating the position of the transform while also using it as the input parameter to the lerp - meaning the translation is no longer linear. To fix this, you just need to keep the inputs constant;

  IEnumerator LerpDash(Vector3 targetPosition, float duration)
  {
      float time = 0;
      Vector3 startPos = transform.position;
      while (time < duration)
      {
          transform.LookAt(targetPosition);
          transform.position = Vector3.Lerp(startPos, targetPosition, time / duration);
          time += Time.deltaTime;
          yield return null;
      }
      transform.position = targetPosition;
  }
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 PlasticFrames · Jun 21, 2021 at 09:03 AM 0
Share

When I add the constraints, the player lerps straight to the final destination and disregards any previously placed destinations altogether.

I've tried setting the startPos outside of the coroutine but it has the same result.

avatar image Namey5 PlasticFrames · Jun 21, 2021 at 10:06 AM 0
Share

That is because you are running all LerpDash instances at the same time (meaning only the last one will have an impact). You would need to either rejig the LerpDash function to include all points or make TriggerDashes a coroutine that yields upon each dash;

  IEnumerator TriggerDashes()
  {
      dashNumber = 0;
      foreach (GameObject gameObject in dashMarks)
      {
          if (dashMarks[dashNumber].transform.position != Vector3.zero)
          {
              yield return LerpDash (dashMarks[dashNumber].transform.position, dashSpeed);
              dashMarks[dashNumber].SetActive(false);
              dashMarks[dashNumber].transform.position = Vector3.zero; //reset position to limit next dashes
              dashNumber++;
          }
      }
  }
avatar image PlasticFrames Namey5 · Jun 21, 2021 at 10:43 AM 0
Share

Thank you so much, it took a little tweaking but it's finally working as intended!

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

129 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 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

StartCoroutine for each object in a foreach() Loop 1 Answer

2D dash in coroutine Inconsistent 1 Answer

Assign RectTransform to Array based on another Array 0 Answers

why does my current code line consist next line of code information 1 Answer

How do I activate game objects in array in timed loop? 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