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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Moving camera through script after animation clip finishes

Discussion in 'Getting Started' started by PlanetaryElephant, Apr 24, 2021.

  1. PlanetaryElephant

    PlanetaryElephant

    Joined:
    Mar 29, 2021
    Posts:
    3
    Hello!!!

    I have been learning Unity for a couple of months and have hit upon a problem that I am sure is easy to solve, but I can't for the life of me find a solution.

    My scene contains a simple spaceship flying over a terrain, and followed by a camera: Screenshot 2021-04-24 at 13.56.19.png

    The script that I use to follow the camera is very simple:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FighterCameraController : MonoBehaviour
    4. {
    5.  
    6.     public Transform camTransform;
    7.     public float camOffsetX = 0.0f;
    8.     public float camOffsetY = 3.0f;
    9.     public float camOffsetZ = -8.0f;
    10.  
    11.     public Transform heroFighter;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         camTransform.position = new Vector3(heroFighter.position.x + camOffsetX, heroFighter.position.y + camOffsetY, heroFighter.position.z + camOffsetZ );
    17.         Debug.Log("Camera Position: X=" + camTransform.position.x + " Y=" + camTransform.position.y + " Z=" + camTransform.position.z);
    18.     }
    19. }
    When the scene starts, I simply set a forward velocity (along the Z-Axis) and the ship moves forward and is followed by the camera.

    This was working great until wanted to add a "swoop in" animation at the start of the scene!

    The first thing I did was add an animator to my project and a simple key frame animation clip that moves the camera from far away and swoops in, and behind the spaceship.

    The problem is that after the animation plays, the script does not appear to have any control of the camera. The ship moves forward, but the camera doesn't follow.



    Any ideas?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    If it were me, I would do it with two separate cameras. Keep your follow cam how it is. Copy down its starting world position. Then create a new camera that does the swooping animation, and ends at your follow cam's starting point. When the swoop cam gets to its end point, disable it and enable the follow cam.

    There's some neat ways to do this using Cinemachine, but I didn't exactly find that easy to learn, either. If you're comfortable doing it in code, keep doing it that way.
     
    PlanetaryElephant and stain2319 like this.
  3. PlanetaryElephant

    PlanetaryElephant

    Joined:
    Mar 29, 2021
    Posts:
    3
    Thanks - I will look into using Cinemachine for this and see if it helps.