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 Ereptor · Jun 21, 2013 at 06:38 AM · c#cameralerptranslatevector3.lerp

I'm using TRANSLATE but want to use LERP. C#

Hi everyone,

I'm working on a project that's going to require a lot of fluid motion. There are going to be documents on a table that will need to move toward the camera when clicked. As of right now, I only have one problem: using a lerp instead of a translate to move my camera.

Here is what is working now:

     void TransitionCamera(){
         Scene.transform.Translate (Vector3.down * 100);
         selectionComplete = true;
     }
     
     void TransitionCameraBack(){
         Scene.transform.Translate (Vector3.up * 100);
         displayScore = true;
     }

Works fine to jump the Scene (including the main camera and the scene background) down and back up again. But I don't want the Scene to jump, I want it to move smoothly over a few seconds. This will make it seem as if objects in the scene are floating upward, and then floating back into the scene when TransitionCameraBack is called.

Can anyone help me? I've tried implementing the Unity documentation on Vector3.Lerp. It didn't help to smooth the transition, I got some errors, I lost a lot of sweat and time, and then I reverted to this original script out of fear.

Thank you a billion for taking the time to read!

Comment
Add comment · Show 2
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 Tarlius · Jun 21, 2013 at 06:45 AM 0
Share

People may find it easier to help if you show the broken code. While we could write a snippet, you're more likely to find out exactly what you were doing wrong that way. It would be particularly useful if you say specifically what errors you got (from what you've said its hard to be sure if you mean compiler errors or runtime errors, for example)

A common pitfall is not setting the t value correctly- it should be between 0 and 1, and vary over time. Lerp also needs to be called each time you want to update the position (so probably every frame the object is moving). The most common way to do that is with a coroutine, which can cause more problems for beginners.

avatar image Ereptor · Jun 21, 2013 at 07:39 AM 0
Share

Well, what I did was mixed this simple above code with some kind of bastardization of the Vector3.Lerp example code. I think this is quite similar to what I was using:

 public GameObject Scene;
     public GameObject SceneTarget;
     
     public Transform start$$anonymous$$arker;
     public Transform end$$anonymous$$arker;
     public float speed = 1.0F;
     private float startTime;
     private float journeyLength;
     public Transform target;
     
     bool moveCameraUp;
     bool moveCameraDown;
     
     void TransitionCamera(){
         startTime = Time.time;
         start$$anonymous$$arker = Scene.transform;
         end$$anonymous$$arker = SceneTarget.transform;
         journeyLength = Vector3.Distance(start$$anonymous$$arker.position, end$$anonymous$$arker.position);
         moveCameraDown = true;
         moveCameraUp = false;
         selectionComplete = true;
     }
     
     void TransitionCameraBack(){
         startTime = Time.time;
         journeyLength = Vector3.Distance(end$$anonymous$$arker.position, start$$anonymous$$arker.position);
         moveCameraDown = false;
         moveCameraUp = true;
         displayScore = true;
     }
     
     void Update() {
         if(moveCameraUp){
             float distCovered = (Time.time - startTime) * speed;
             float fracJourney = distCovered / journeyLength;
             transform.position = Vector3.Lerp(start$$anonymous$$arker.position, end$$anonymous$$arker.position, fracJourney);
         }
         if(moveCameraDown){
             float distCovered = (Time.time - startTime) * speed;
                 float fracJourney = distCovered / journeyLength;
             transform.position = Vector3.Lerp(end$$anonymous$$arker.position, start$$anonymous$$arker.position, fracJourney);
         }
     }

2 Replies

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

Answer by whydoidoit · Jun 21, 2013 at 07:42 AM

You could try this:

         IEnumerator TransitionCamera()
         {
                 var t = 0f;
                 var start = Scene.transform.position;
                 
                 while(t < 1)
                 {
                          t += Time.deltaTime;
                          Scene.transform.position = Vector3.Lerp(start, start + Vector3.down * 100, t);
                          yield return null;

 
                 }
                 selectionComplete = true;
         }


Then call it using:

          StartCoroutine(TransitionCamera());
Comment
Add comment · Show 5 · 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 Ereptor · Jun 21, 2013 at 01:41 PM 0
Share

I'm unfamiliar with the I() calls like IEnumerator. Is that a class? Does it go outside of Start() like TransitionCamera would? All I know about it is that it is a "coroutine", but I don't know what that means.

I'll plug it in and see if it works. Thank you again for your help!

avatar image whydoidoit · Jun 21, 2013 at 01:49 PM 0
Share

It's what is known as an interface (the specification of some functions that must be implemented, it basically allows an object to provide multiple common functions). In the case of Unity it uses IEnumerator to run coroutines - routines that run across multiple frames of the game.

The yield return null; causes the routine to pause until the next frame.

avatar image Ereptor · Jun 21, 2013 at 01:54 PM 0
Share

By the way, it worked! Woo! Thank you!

avatar image whydoidoit · Jun 21, 2013 at 09:43 PM 0
Share

Could you tick the answer for me?

avatar image gulfam131 · Jan 31, 2019 at 01:53 PM 0
Share

Thanks, it works, you saved a lot of our time.

avatar image
1

Answer by Bunny83 · Jun 21, 2013 at 07:50 AM

I don't use it that much myself, but i should mention it:

iTween

which is a free library and offers tons of lerping and tweening functions and is very easy to use. In most cases i implement simple tweens like whydoidoit showed in his answer because you have more control ;)

Comment
Add comment · 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

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

18 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

Related Questions

Vector3.Lerp moves in the wrong direction 1 Answer

Slide object between two moving points c# 1 Answer

Smoothly Rotate Character Y to Camera Y 2 Answers

MouseWheel Lerp Smoothing Problem 1 Answer

First Person Character, Mecanim. 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