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 /
  • Help Room /
This question was closed Oct 30, 2016 at 03:29 AM by TienCuong for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TienCuong · Sep 04, 2016 at 10:36 AM · coroutinelerp

Why my angle lerp code take only 30% time to finish?

Hello there, I'm trying making a game where camera slowing lerp to look at my ship, not look at it all the time instantly. My issue is, why my code only take 30% time to finish ? I know this because i put a Debug.Log there to count (elapseTime/time) and it only increase to 0.3 and the lerp finished, although the debug still continue to count to 1. Pls help enlighten me masters !

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour {
 
     public Transform target ;
     Transform camFollowPos;
     float smooth = 3f;
     // Use this for initialization
     void Start () {
         camFollowPos = target.FindChild ("camFollowPos");
     }
 
     // Update is called once per frame
     void Update () {
 
         transform.position = camFollowPos.position;
         if(Input.GetKeyDown(KeyCode.Space))
             {
                 StopCoroutine(LookatShipCoroutine(smooth));
                 StartCoroutine(LookatShipCoroutine(smooth));
             }
     }
 
     IEnumerator LookatShipCoroutine(float time)
     {
         float elapsedTime = 0f;
         while (elapsedTime < time) {
             transform.rotation = Quaternion.Lerp( transform.rotation,camFollowPos.rotation,(elapsedTime/time));
             elapsedTime += Time.deltaTime;
             Debug.Log (elapsedTime / time);
             yield return null;
         }
     }
 }

Comment
Add comment · Show 4
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 Hellium · Sep 04, 2016 at 10:40 AM 1
Share

http://answers.unity3d.com/questions/998544/having-difficulty-with-lerp.html#answer-998744

Two solutions

 IEnumerator LookatShipCoroutine(float time)
  {
     Quaternion startRotation = transform.rotation ;
     Quaternion endRotation = camFollowPos.rotation ;
     
      for( float elapsedTime = 0f ; elapsedTime < time ; elapsedTime += Time.deltaTime )
      {
          transform.rotation = Quaternion.Lerp( startRotation,endRotation,(elapsedTime/time));
          Debug.Log (elapsedTime / time);
          yield return null;
      }
  }


  IEnumerator LookatShipCoroutine(float time)
  {
     Quaternion endRotation = camFollowPos.rotation ;
     
      for( float elapsedTime = 0f ; elapsedTime < time ; elapsedTime += Time.deltaTime )
      {
          transform.rotation = Quaternion.Lerp( transform.rotation,endRotation, Time.deltaTime );
          Debug.Log (elapsedTime / time);
          yield return null;
      }
  }
avatar image TienCuong Hellium · Sep 04, 2016 at 11:04 AM 0
Share

@Hellium Hi man, thanks for the answer ! Can you explain how your two solutions different than each other and my code ? I want to know more deeply. Why in your first solution the t = elapsedTime/time ( from 0 to 1 ) , but in the second -> the t = Time.deltaTime , i think this Time.deltaTime is always close to 0.02 ?

avatar image doublemax TienCuong · Sep 04, 2016 at 12:01 PM 1
Share

I think the main difference is that you shouldn't use transform.rotation inside the Lerp, because its value changes on each iteration.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

MoveTowards and Lerp not working 1 Answer

Creating points for robot (End Effector) to go through them 0 Answers

Weapon does't meet the target position when using IK 1 Answer

Lerp between two int vaules smoothly 4 Answers

Control lerps with if-statemnts in Update or with Coroutines? 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