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 smirlianos · Aug 29, 2014 at 06:44 PM · raycastpositionlerpscreenpointtoray

Lerp isn't completing itself

Hello,

I have this script that moves the player to the mouse position when clicked. The problem is that the lerp is not completed, and the character moves only a bit after each click. Any help?

 if (Input.GetMouseButtonDown(0))
     {
         var hit: RaycastHit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if (Physics.Raycast(ray, hit)) {
             transform.position = Vector3.Lerp (transform.position, hit.point, Time.deltaTime * smooth);
         }
     }
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
1

Answer by tanoshimi · Aug 29, 2014 at 06:48 PM

This is a very common mistake. Assuming this code is within Update() then, on the frame in which your mouse button is clicked, you are moving "Time.deltaTime * smooth" (a small number) of the fraction of the distance from the current position to the hit point. That's it.

It's unclear what behaviour you really meant, but try changing GetMouseButtonDown to GetMouseButton. Then hold the button down to move.

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 smirlianos · Aug 29, 2014 at 06:59 PM 0
Share

yes, in this case it works, but I need to move with one click

avatar image
0

Answer by KMKxJOEY1 · Aug 29, 2014 at 06:48 PM

 if (Input.GetMouseButtonDown(0))
     {
         var hit: RaycastHit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
         if (Physics.Raycast(ray, hit)) {
             StopAllCoroutines();
         StartCoroutine(Motion(hit.point));
         }
     }

and then add this method:

 IEnumerator Motion(Vector3 position)
 {
     float t = 0;
     while(t < 1)
     {
         t += Time.deltaTime;
         transform.position = Vector3.Lerp (transform.position, position, Time.deltaTime * smooth);
         yield return null;
     }
 }

This will move for 1 second, and should be enough depending on your smooth factor.

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 smirlianos · Aug 29, 2014 at 07:01 PM 0
Share

Do I put the second part outside of the Update function? Because if I do, I get errors!

avatar image smirlianos · Aug 29, 2014 at 07:02 PM 0
Share

$$anonymous$$aybe because it is C# syntax (I think) Can you translate it to Js?

avatar image froYo · Aug 30, 2014 at 01:08 AM 0
Share
 function Update ()
 {
     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
         if (Physics.Raycast(ray, hit)) 
         {
             StopAllCoroutines();
             $$anonymous$$otion(hit.point);
         }
     }
 }
 
 function $$anonymous$$otion(position : Vector3)
 {
     var t : float = 0;
     
     while(t < 1)
     {
         t += Time.deltaTime;
         transform.position = Vector3.Lerp (transform.position, position, Time.deltaTime * smooth);
         yield;
     }
 }


"Should" be translated into .js if i haven't failed :)

avatar image smirlianos · Aug 30, 2014 at 09:34 AM 0
Share

thanks, you didn't fail!

avatar image
0

Answer by kacyesp · Aug 29, 2014 at 06:49 PM

You could add an if statement saying if the distance from the hit point is ridiculously small, say .001f, then set the transform's position equal to the hit point.

Comment
Add comment · Show 2 · 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 KMKxJOEY1 · Aug 29, 2014 at 06:50 PM 0
Share

This is incorrect. Lerp will interpolate the Vectors and will reach the destination if you plug in the right ratio value $$anonymous$$G. 1. $$anonymous$$any people have a misunderstanding of how Lerp and SLerp work.

avatar image kacyesp · Aug 29, 2014 at 06:53 PM 0
Share

Oh I didn't know that, thanks. I edited my answer.

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

24 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

Related Questions

Not getting right value; ScreenPointToRay 0 Answers

How can I smooth the rotation using quaternion and raycast? 1 Answer

Move character's position to middle of object 1 Answer

Using Trigonometry for Collision Detection 0 Answers

how to tell what platform you're on. 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