Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Smooth camera transition.

Discussion in 'Getting Started' started by skuky123, Feb 7, 2021.

  1. skuky123

    skuky123

    Joined:
    Feb 7, 2021
    Posts:
    14
    Hello to every1!

    Im new in the unity, just learning a lot last few days. But as always I need some help.

    I created some kind of moving ball game in short. My camera is following player for now, so what I need. I need on start of the game the camera stays top down from my level, and smoothly comes to the player, and contiune following player afterwards. So I guess its code that smoothly transits (lets say) starting camera or default to the player following again. I hope you understood me. Sorry for my bad english!

    Greetings and thanks

    Kristian
     
  2. fusefactory

    fusefactory

    Joined:
    Jul 23, 2018
    Posts:
    2
    You should look for some lerping functions! basically what you need is to linear interpolate (lerp) from one transform to another.
    you should add an empty gameobject and put it in the position you want the camera to start (also with the correct rotation), then you can lerp from that transform to the current camera transform that is following the player.
    In order to lerp position you can use Vector3.Lerp(transform1.position, transform2.position, lerpCoefficient) while to lerp the rotation you can use Quaternion.Lerp(transform1.rotation, transform2.rotation, lerpCoefficient) and then assign those values to the camera transform.
    If at start the lerp coefficient is 0.0, the camera will be in the exact position and rotation of transform1 (your empty gameobject).
    You can slowly increase the lerp coefficient during the Update() function and when it will reach 1.0 you will get the position and rotation of transform2.
    all values between 0.0 and 1.0 will yield positions and rotations in between the two transforms, giving you that smooth transition that you are looking for.
     
    Esha_M, Schneider21 and skuky123 like this.
  3. skuky123

    skuky123

    Joined:
    Feb 7, 2021
    Posts:
    14
    Thanks man, can you write please how code would look like. I understand what I should do but I dont understand how code should be written. Thanks!
     
  4. skuky123

    skuky123

    Joined:
    Feb 7, 2021
    Posts:
    14
    I did it! Thanks man! Thmubs up!
     
  5. fusefactory

    fusefactory

    Joined:
    Jul 23, 2018
    Posts:
    2
    That's awesome! glad I could help :)
     
    skuky123 likes this.