Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 coryl · Jun 29, 2011 at 10:01 PM · raycastlerpvector2

Difficulty with Vector2 Lerp, object jumping, not moving

Hello friends, I'm trying to make my game object move from its current start position to where the user clicks on the screen. I've used a ScreenPointToRay to help find the position of where they click.

The basic problem I'm having is I'm unsure how to manipulate the Lerp function to smoothen out my movement. Right now, the game object is simply jumping to where the user clicks. Its not translating or moving, just appearing suddenly. I suspect the "t" variable of the Lerp function is something to do with it, but I don't understand what it means. Any ideas? Thanks

    function Update () {
 
     var cube = GameObject.FindWithTag("Cube");
     
     if(Input.GetMouseButtonDown(0)){
      var startPos = cube.transform.position;
      var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
      var selected : RaycastHit;
 
      Physics.Raycast(ray, selected);
      cube.transform.position = Vector2.Lerp(startPos, selected.point, Time.time);            
         
     }
 }
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

2 Replies

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

Answer by Eric5h5 · Jun 29, 2011 at 10:14 PM

That's not how Lerp is used; it's not a function that makes things happen over a period of time. Lerp returns a value immediately. (Also, you don't want Vector2, which only has two vectors. You need 3 vectors for 3D space.) In order to make things appear to happen over a period of time, you call Lerp repeatedly, while gradually advancing the third parameter from 0.0 to 1.0. See here for a coroutine that moves an object from one point to another.

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 coryl · Jun 29, 2011 at 10:34 PM 0
Share

Thanks eric. I'm actually only translating on the X and Y axis, so I don't need Vector3. I'm using your $$anonymous$$oveObject function successfully.

avatar image
0

Answer by Skjalg · Jun 29, 2011 at 10:14 PM

When using Vector2.Lerp the time (the third variable) needs to be a float between 0 and 1. Using Time.time after 15 seconds of gametime will result in the number 15 being thrown in there, and will ultimately result in the object just popping up at the end position.

What you probably want to do is using Time.deltaTime multiplied by some speed factor.

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 Eric5h5 · Jun 29, 2011 at 10:19 PM 1
Share

You usually don't want to use Time.deltaTime multiplied by some speed factor; see here: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get RaycastHit Local Coords (C#) 1 Answer

Collisions while using Vector2.Lerp 0 Answers

Moving Object using Raycast / Lerp C# 2D 0 Answers

Problem with lerping Y axis 0 Answers

[C#] can someone give me a detailed explanation of lerp 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