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 Fluzing · Apr 08, 2013 at 02:12 PM · lerpmovetowards

Lerp or MoveTowards not working

I am trying to make a single selected cube move to a certain point. It works fine if I just use transform.position = pointOfImpact, but as soon as I try to use Lerp or MoveTowards, the cube just sits there doing nothing. The frustrating part it, the code worked fine in a different project.

Can anybody see what I'm doing wrong?

 var smooth:int;
 
 function Update () {
     
     if(Input.GetKey(KeyCode.Mouse1)){
     var mousePosition : Vector3 = Input.mousePosition;
     var screenRay : Ray = Camera.main.ScreenPointToRay( mousePosition );
 
     var hitInfo : RaycastHit;
 
     Physics.Raycast( screenRay, hitInfo );
 
     var pointOfImpact : Vector3 = hitInfo.point;
     pointOfImpact = Vector3(Mathf.Round(pointOfImpact.x), 0.5f, Mathf.Round(pointOfImpact.z));
 
     transform.position = Vector3.Lerp (transform.position, pointOfImpact, Time.deltaTime * smooth);
     } 
Comment
Add comment · Show 3
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 whydoidoit · Apr 08, 2013 at 02:39 PM 0
Share

You are only calling the Lerp if the mouse button is down - did you only want it to move while the button is down?

avatar image Charles_Gams · Apr 08, 2013 at 03:39 PM 0
Share

Looking at your code your multiplying smooth which is just an integer and has no value i don't know if that maybe be the issue of actually stating what value smooth is

avatar image fafase · Apr 08, 2013 at 03:59 PM 0
Share

You gave a value to smooth didn't you?

1 Reply

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

Answer by Dracorat · Apr 08, 2013 at 04:21 PM

I believe what you want is more like this: (Please excuse any syntax error - I'm writing off the top of my head)

One movement allowed at a time:

 class Something : MonoBehavior {
     //This would be attached to the object itself.
     private float timeToDestination = 2f; // the f makes the 2 a float
     private bool currentlyAnimating = false;

     public void Update() {
         if(Input.GetMouseButtonDown(0) && !this.currentlyAnimating){
             StartCoroutine(MoveThis(Input.mousePosition));
         }
     }

     private IEnumerator MoveThis(Vector3 destinationPosition) {
         Vector3 startPosition = this.transform.position;
         this.currentlyAnimating = true;
         float scale = 1f / this.timeToDestination;
         float i = 0f;
         while(i < 1f){
             i += Time.deltaTime * scale;
             this.transform.position = Vector3.Lerp(startPosition, destinationPosition, i);
             yield return null;
         }
         this.currentlyAnimating = false;
     }
 }

Allow movement to change in the middle

 class Something : MonoBehavior {
     //This would be attached to the object itself.
     private float timeToDestination = 2f; // the f makes the 2 a float
     private bool currentlyAnimating = false;
     private object threadLock = new object();
     private Vector3 destinationPosition;
     private Vector3 startPosition;
     private float i = 0f;
 
     public void Update() {
         if(Input.GetKey(KeyCode.Mouse1)){
             bool startNeeded = false;
             lock(this.threadLock){
                 this.i = 0f;
                 this.startPosition = this.transform.position;
                 this.destinationPosition = Input.mousePosition;
                 startNeeded = this.currentlyAnimating == false;
             }
             if(startNeeded) StartCoroutine(MoveThis());
         }
     }
 
     private IEnumerator MoveThis() {
         lock(this.threadLock){
             this.currentlyAnimating = true;
         }
         float scale = 1f / this.timeToDestination;
         while(i < 1f){
             lock(this.threadLock){
                 i += Time.deltaTime * scale;
                 this.transform.position = Vector3.Lerp(startPosition, destinationPosition, i);
             }
             yield return null;
         }
         lock(this.threadLock){
             this.currentlyAnimating = false;
         }
     }
 }
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 fafase · Apr 08, 2013 at 04:29 PM 0
Share

$$anonymous$$oveThis should be IEnumerator if coroutine. But still this is a little $$anonymous$$sing me, why using a for loop when a while loop seems way more appropriate? Also, you have a couple of typos like transform destinationPosition should be Transform.

Here is a more simple version:

 IEnumerator $$anonymous$$oveThis(Vector3 destinationPosition, float period) {
   var t = 0f;
   var startingPosition = objectTo$$anonymous$$ove.position;
   while(t < 1){
         transform.position = Vector3.Lerp(transform.position, destinationPosition, t);
         t += Time.deltaTime / period;
         yield return null;
   }
 }
avatar image Dracorat · Apr 08, 2013 at 04:31 PM 0
Share

I agree on the while loop so I added it. I already corrected all the rest while you were typing =)

avatar image fafase · Apr 08, 2013 at 04:43 PM 0
Share

I see one issue yet, I think the OP expects the object to move to the hit point but also to follow if the position changes (chaneg target), hence the use of Get$$anonymous$$ey. Your code will wait for the move to be done before he can start a new move. Or it needs a coroutine cancel logic.

avatar image Dracorat · Apr 08, 2013 at 04:49 PM 0
Share

Edit: Actually, I just added the other code I described in detail. Now the OP can choose which he likes.

avatar image Fluzing · Apr 08, 2013 at 09:38 PM 0
Share

OP here. Thank you all for replying. I want to selected object to move on a grid like in a top down RPG. Once you select the place to move to, it does not change while moving. The Get$$anonymous$$ey was a mistake. Is the code you gave me in Javaor C#? I don't recognize some of it. (I'm a bit of a coding rookie still).

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

15 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

Related Questions

Leading Reticle 0 Answers

Smooth swapping my game object 0 Answers

How to move an object from another position using Vector3.moveTowards precisely and slowly by swiping (mobile) 1 Answer

3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers

Move an Object from Point A to relative Point B 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