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 inglipX · May 12, 2013 at 09:25 PM · transformlerpsmooth

Smooth Lerp movement?

Currently it just teleports the player to the position of the empty object. How can i make it smoothly move to the empty object?

 var touchingLedge : boolean;
 var player : GameObject;
 var end : Transform;
 var start : Transform;
 
 function Update () {
 
     if (touchingLedge == true)
         player.transform.position = Vector3.Lerp(start.position, end.position, Time.time);
         
 }
 
 function OnTriggerEnter (hit : Collider)
 {
  if (hit.gameObject.tag == "Player")
      touchingLedge = true;
      yield WaitForSeconds(0.3);
      touchingLedge = false;
 
 }
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
2
Best Answer

Answer by greenshadow · May 13, 2013 at 01:06 AM

Here's a solution I ran across some time ago, likely from these boards. Lerp over time. I think it will work as you want it to:

 //Declared somewhere near touchingLedge
 var moving : boolean = false;

 function Update () {
     if (touchingLedge == true) {
         //Last variable is seconds you want Lerp to last
         StartCoroutine(MoveFromTo(start.position, end.position, 0.5f));
     }
 }

 function MoveFromTo(pointA : Vector3, pointB : Vector3, time : float) {
     if (!moving) {                     // Do nothing if already moving
         moving = true;                 // Set flag to true
         var t : float = 0f;
         while (t < 1.0f) {
             t += Time.deltaTime / time; // Sweeps from 0 to 1 in time seconds
             transform.position = Vector3.Lerp(pointA, pointB, t); // Set position proportional to t
             yield;         // Leave the routine and return here in the next frame
         }
         moving = false;             // Finished moving
     }
 }
Comment
Add comment · Show 6 · 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 inglipX · May 13, 2013 at 01:12 AM 0
Share

Thank you for your response! I will test it out tommorow when i am able. Thank you!

avatar image inglipX · May 14, 2013 at 08:10 PM 0
Share

Hello, i get an error It appears to be "yield return 0" I don't know why :

Assets/Standard Assets/Scripts/ClimbWallTransform.js(25,15): UCE0001: ';' expected. Insert a semicolon at the end.

 var touchingLedge : boolean;
 var player : Transform;
 var end : Transform;
 var start : Transform;
  
 //Declared somewhere near touchingLedge
 var moving : boolean = false;
  
 function Update () 
 {
     if (touchingLedge == true) 
     {
        //Last variable is seconds you want Lerp to last
        StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, 0.5f));
     }
 }
  
 function $$anonymous$$oveFromTo(start : Vector3, end : Vector3, time : float) {
     if (!moving) {               // Do nothing if already moving
        moving = true;           // Set flag to true
        var t : float = 0f;
        while (t < 1.0f) {
          t += Time.deltaTime / time; // Sweeps from 0 to 1 in time seconds
          transform.position = Vector3.Lerp(start, end, t); // Set position proportional to t
          yield return 0;      // Leave the routine and return here in the next frame
        }
        moving = false;        // Finished moving
     }
 }
 
 function OnTriggerEnter (hit : Collider)
 {
  if (hit.gameObject.tag == "Player")
     touchingLedge = true;
     
  
     }
 
avatar image greenshadow · May 14, 2013 at 08:19 PM 0
Share

@ inglipX: I converted this over to JS from one of my C# projects, and I missed the yield statement. Sorry about that.

Try replacing this:

 yield return 0;

With this:

 yield;
avatar image inglipX · May 14, 2013 at 08:43 PM 0
Share

okay thank you it works. Now how am i able to adjust the speed at which i move?

avatar image greenshadow · May 14, 2013 at 09:04 PM 0
Share

@inglipX: To change the speed at which you move, you have to figure the duration you want the move to last, and pass that in as the last variable when you call the routine.

So for example, this call moves the object over half a second duration:

 //Last variable is seconds you want Lerp to last
 StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, 0.5f));

This call would move the object over 2 seconds:

 //Last variable is seconds you want Lerp to last
 StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, 2f));

So if the distance is going to vary, and you want the speed of the object to always be the same, and the duration to be based on the distance between the two points, you could do something like this:

 function Update ()
 {
     if (touchingLedge == true)
     {
         //Get the distance between the objects
         var distance : float = Vector3.Distance(end.position, start.position);
 
         //Scale the duration down
         var duration : float = distance * 0.1f;

         //Last variable is seconds you want Lerp to last
         StartCoroutine($$anonymous$$oveFromTo(start.position, end.position, duration));

     {

 {

If you use the last approach, you may have to tinker with this line to get it working how you like it:

 //Scale the duration down
 var duration : float = distance * 0.1f;

You could move faster by multiplying the distance by a lower number, like this:

 //Scale the duration down
 var duration : float = distance * 0.05f;

Or you may like it to be a bit slower, by multiplying by a higher number:

 //Scale the duration down
 var duration : float = distance * 0.2f;

It really all depends how much distance is between the objects in your game, and how fast you want the objects to move.

Show more comments

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

16 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

Related Questions

Animationproblem with Lerp-Function 1 Answer

A node in a childnode? 1 Answer

smooth fps movements using joystick 1 Answer

How to ROTATE an object without slowing the ends (lerp) 3 Answers

Move position with smoothing 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