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
0
Question by Dok101 · Dec 24, 2015 at 10:18 AM · vector3lerp

Problem with Vector3.Lerp

Hello Right now I am trying to use Vector3.Lerp to slide in a panel as part of my menu. However currently it returns a weird value overtime I try to do it. Here is my code using UnityEngine;

using System.Collections; using UnityEngine.UI;

public class MenuAnimations : MonoBehaviour {

 public int WhichPanel;
 public GameObject Panels;
 public Transform Panel1Vector;
 public Transform Panel2Vector;
 public Transform Panel3Vector;
 public Transform Panel4Vector; 
 public float speed;

 public void MovePanel (int WhichPanel)
 {
     switch (WhichPanel)
     {
         case 0:
         Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel1Vector.position, speed);
         Debug.Log(Panel1Vector.position);
         Debug.Log(Panels.transform.position);
         break;

         case 1:
         Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel2Vector.position, speed);
         Debug.Log(Panel2Vector.position);
         Debug.Log(Panels.transform.position);
         break;

         case 2:
         Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel3Vector.position, speed);
         Debug.Log(Panel3Vector.position);
         Debug.Log(Panels.transform.position);
         break;

         case 3:
         Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel4Vector.position, speed);
         Debug.Log(Panel4Vector.position);
         Debug.Log(Panels.transform.position);
         break;
     }
 }

}

For example, the Panel1Vector should be (125, 0, -8) but for some reason if I press my button to make WhichPanel = 0, the GameObject Panels goes to (369.1433, 0, -289.4084).

Does anyone know how to fix this or what my problem is.

Thank you

Dok

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
1
Best Answer

Answer by LazyElephant · Dec 24, 2015 at 11:03 AM

Unless you're varying the value of speed somewhere else, you're not using Vector3.Lerp correctly. Lerp works by returning the interpolation of the line between the two given vectors based on the third parameter, which should be between 0 and 1.

When you pass 0 to the Lerp function, it returns the first Vector3 argument. When you pass 1, it returns the 2nd Vector argument. If you pass 0.5, it returns a Vector3 half way between the two. So, in order to get a smooth transition between two Vector3s, you have to vary the 3rd argument over time. For example:

Vector3 pos = Vector3.Lerp( vector1, vector2, (Time.deltaTime / HowManySecondsYouWantItToTake) );

http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

As an alternative to Lerp, you could use Vector3.MoveTowards, which works in the way you are trying to use Lerp.

http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

You could also do all of this using animations. If you have time, I recommend this playlist by 3dBuzz on youtube. It covers a lot of information on UI development in a clearly explained way.

https://www.youtube.com/playlist?list=PLt_Y3Hw1v3QTEbh8fQV1DUOUIh9nF0k6c

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 Dok101 · Dec 24, 2015 at 12:06 PM 0
Share

Thank you for your answer I was able to fix the problem by making the Panel have the same parent as the Transforms Thank you

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

33 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.lerp and then wait 1 Answer

Vector3.Lerp result in "laggy" movement while running on iOS devices 2 Answers

Start Camera Lerp from diffrent script? 1 Answer

Rotating objects around player using lerp 0 Answers

How would I smooth out a Quaternion? 2 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