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 zakarykinnaird · Mar 20, 2012 at 05:11 AM · vector3lerpmoveonmousedownbreak

Stop a Lerp from looping

The lerp on this script runs every update, how do I tell it to stop when its reached its lerp end point?

 var toggle : int = 1;
 var moving : boolean = false;     //uses toggle to become true or false
 var speed : float = 1;            //speed of movement
 var start : Vector3;
 var end : Vector3;
 
 function Update () {
     
     if(moving == true) {
     start = Vector3(transform.position.x,transform.position.y,0);
     end = Vector3(transform.position.x,transform.position.y,1);
     }else{
     start = Vector3(transform.position.x,transform.position.y,1);
     end = Vector3(transform.position.x,transform.position.y,0);
     }
     
     //Moves Gameobject
     if(moving) {
     transform.position = Vector3.Lerp(start, end, Time.deltaTime*speed);
     }
     
     //Controls Active from toggle - Active starts false
     if(toggle == 1) {
     moving = false;
     }else{
     moving = true;
     }
     
 }
     
 function OnMouseDown() {
     toggle = (toggle + 1)%2;
     }
     
 //GameObejct need Collider
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
0

Answer by DaveA · Mar 20, 2012 at 05:21 AM

One way is to use Mathf.Approximately on x,y,z to see if the current position is near the destination. Or you can use a distance check to see if it's close enough.

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 Kleptomaniac · Mar 20, 2012 at 06:37 AM

Why not just add on to your if statement:

 if(moving && transform.position != end) {

That will stop the lerp from running when the transform.position of the gameObject is equal to the end point. :)

Hope that helps, Klep

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 Meltdown · Mar 20, 2012 at 06:42 AM 0
Share

Checking if two floating point values are identical is not a very efficient thing to do, and in fact they might never ever match, or take longer than usual to match.

avatar image Kleptomaniac · Mar 20, 2012 at 06:54 AM 0
Share

Hmm, yeah true ... @DaveA 's answer is probably best then. :P

avatar image syclamoth · Mar 20, 2012 at 07:03 AM 0
Share

Well, that's what

 $$anonymous$$athf.Approximately (f1, f2)

exists for!

avatar image
0

Answer by Meltdown · Mar 20, 2012 at 06:43 AM

You can do a simple distance proximity check..

i.e

 var distance = Vector3.Distance(transform.position, end);
 
 if(distance < 0.1f)
   moving = false;
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 zakarykinnaird · Mar 20, 2012 at 09:55 PM 0
Share

Thanks guys that gives me lots of options, I did want to add a collision to the movement, so if it bumps into another object it stops and moves back to its start point (which will always be transform.position.x,transform.position.y,0).

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ricreate Lerp function 2 Answers

Vector3.lerp doesn't work 1 Answer

Problem with Vector3.Lerp 1 Answer

Move Object Forward Click, Move Object Back again on another Click 0 Answers

Move character by X units on the left 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