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 Woj_Gabel_FertileSky · Mar 22, 2012 at 11:34 PM · lerpdistancelinerendererinterpolate

Get positions at equal intervals between two Vector3

Hi!

I want to divide lineRenderer to some n parts and get vector3 positions between start position and and position. Right now I'm trying to use Vector3.Lerp for it in a for loop, but cant get the last value of lerp function right. Any help?

 var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
             
 var layerBuildMask2  = 1 << 21;    
 var layerCritMask2  = 1 << 22;
 var combinedLayer2 = layerBuildMask2 | layerCritMask2;
         
 var lineRenderer : LineRenderer = gameObject.GetComponent("LineRenderer");
         
 var linePos : int = 16;
 lineRenderer.SetVertexCount(linePos);
         
 if (Physics.Raycast (ray, hit, Mathf.Infinity, combinedLayer))
     {
     var distance=Vector3.Distance(selectedBuilding.transform.position,hit.transform.position);
  
        for(s = 0 ; s<linePos ; s++)
        {
         lineRenderer.SetPosition(s, Vector3.Lerp(hit.transform.position,selectedBuilding.transform.position,distance/s));
        }
 
     }
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

3 Replies

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

Answer by Owen-Reynolds · Mar 22, 2012 at 11:56 PM

Lerp expects a 0-1 percent as the last input, so just give it 0/15, 1/15, 2/15 ... 15/15. I think the confusion is that most people don't have a 0-1, so have to do math to force it. You're using lerp for the exact thing it was meant to do:

 for(int s=0;s<linePos;s++) {
   ... lerp(start, stop, s/(linePos-1)) ...
   // EX: when s=5, you get 5/15ths, so the point is 1/3rd of the way over

The extra "-1" is because you want to start at 0/15ths and end at 15/15ths (equals 100%.) That's a total of 16 points.

Comment
Add comment · Show 4 · 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 Woj_Gabel_FertileSky · Mar 23, 2012 at 07:53 AM 0
Share

Thank You! I will try this after work.

avatar image Woj_Gabel_FertileSky · Mar 23, 2012 at 05:23 PM 0
Share

Eh, it's not working. It's always the same. All positions that are set are the same and the last is the pos of the target. So I am always getting a line with 15 points in one spot and the last spot is in the targetBuilding position. So no linear interpolation. If i divide the last value in interpolation by some big number(100) its getting uneven interpolated positions.

avatar image Woj_Gabel_FertileSky · Mar 23, 2012 at 05:40 PM 0
Share

Ok, so s is int, so just change this val to some float and we are golden :D

         for(s = 0 ; s<linePos ; s++)
         {
             
             lineRenderer.SetPosition(s, Vector3.Lerp(hit.transform.position,selectedBuilding.transform.position,x/(linePos-1)));
             Debug.Log("s="+s+":"+s/(linePos-1));
             x++;
             
         }
         x=0;

where x is a float.

avatar image Owen-Reynolds · Mar 23, 2012 at 10:38 PM 0
Share

Or, (don't generally use javascript) ((float)(s)) should allow you to keep s as an int, or a hack: (1.0*s).

avatar image
1

Answer by Kiloblargh · Mar 23, 2012 at 05:47 PM

Divide by zero may be killing the rest of the for loop, and "distance/s" wrong.

Try this:

 var targetPos : Vector3 = selectedBuilding.transform.position;
 Debug.Log (targetPos);
 var clickPos : Vector3 = hit.transform.position;
 Debug.Log (clickPos);
 lineRenderer.SetPosition(0,clickPos);
 for (var s : int = 1; s < linePos; s++)
     {
     Debug.Log(s);
     var percentage : float = s/linePos;
     Debug.Log(percentage);
     var splitPos : Vector3 = Vector3.Lerp(clickPos,targetPos),percentage);
     Debug.Log(splitPos);
     lineRenderer.SetPosition(s, splitPos);
     }
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
avatar image
0

Answer by farooqaaa · Mar 23, 2012 at 12:27 AM

The last value of Vector3.Lerp() smoothing. The higher its value the faster the object will interpolate to the given position. Use "0" for maximum smoothing and "1" for minimum smoothing.

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 Owen-Reynolds · Mar 23, 2012 at 03:44 PM 1
Share

If you use the form where each frame your new value is a lerp starting from your old value: value=Lerp(value, endValue, x); then, yes, x is the speed/smoothFactor.

But the OP is using Lerp for it's "real" use -- linear interpolation between two points.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change a value over specified time? 2 Answers

Find center between 4 points 3 Answers

Linear interpolation crash 0 Answers

Vector3.Lerp - Constant speed between distance changes 3 Answers

Coroutine or Distance Calc? 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