- Home /
Having Trouble animating camera at start of game
I am trying to have it so that when the game starts my camera starts facing the player character then animates to the top down perspective. This is a top down endless runner type game. At the start the player is already moving on the z axis. So the camera is transitioning while following the player. Here is how I "think" it should look. But I need help finishing it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
private Transform lookAt;
private Vector3 startOffset;
private Transform camerasubject;
private float animationTransition = 0.0f;
private float animationDuration = 4.0f;
// Use this for initialization
void Start ()
{
CameraAnimation ();
lookAt = GameObject.FindGameObjectWithTag ("Player").transform;
startOffset = transform.position - lookAt.position;
}
// Update is called once per frame
void LateUpdate ()
{
transform.position = lookAt.position + startOffset ;
}
void CameraAnimation()
{
camerasubject= GameObject.FindGameObjectWithTag ("Player").transform;
// Get player position then move camera around it.
}
}
Answer by Cornelis-de-Jager · Jul 05, 2017 at 03:24 AM
Remove the CameraAnimation () function. it is redundant.
Add the following variables and replace the code in LateUpdate with the following (will explain why after):
// Speed variable
float transitionTime = 10f;
float transitionSpeed = 0.001f;
void LateUpdate () {
// Move the camera
transform.position = Vector3.Lerp (transform.position, lookAt.position + startOffset, transitionTime);
// Reduce the transition time to avoid lag later
if (transitionTime > 1f)
transitionTime -= transitionSpeed ;
// Ensure subject is kept
transform.lookAt (lookAt);
}
The above script will ensure that the transition time is slow at first then gets faster so that there is no lag between the camera following and the player movement like there will be in the start.
The camera doesn't transition it just stays in front of the player as he moves forward
Your answer
Follow this Question
Related Questions
How to run 1 animation, then other 2 Answers
How to make animations match movement direction 0 Answers
Fixed camera on rails. 2 Answers
how to stop the camera to follow the player on his jump movement ? 2 Answers
Follow animation position 2 Answers