Search Unity

Transition between animator states smoothly

Discussion in 'Animation' started by DKrazy, Jul 10, 2022.

  1. DKrazy

    DKrazy

    Joined:
    Oct 23, 2021
    Posts:
    10
    I'm making a 3D first person game. I have it set so that, when walking, the camera sort of bobs up and down and strafes left and right, and when still, the camera also remains still. However, I've run into the problem that when the character stops, the camera will immediately snap back into place. It looks really bad, and I'm trying to fix it.

    I can't just use another animation, because the position of the camera when the character stops moving is a variable. I've looked into stuff like Lerp, but I'm having a hard time wrapping my head around it. Could anyone explain to me what it is I need to do?

    Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraAnimations : MonoBehaviour
    6. {
    7.     Animator cameraAnimator;
    8.  
    9.     [SerializeField] PlayerMovement controller;
    10.  
    11.     private void Start()
    12.     {
    13.         cameraAnimator = GetComponent<Animator>();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if (controller.walking)
    19.         {
    20.             cameraAnimator.Play("Walking");
    21.         }
    22.         else
    23.         {
    24.             cameraAnimator.Play("Idle");
    25.         }
    26.     }
    27. }
    28.