I am working on some kind of temple run game project just for the educational purpose. And I have created a script which should focus on character from little bit high and then change its position to characters. Like in temple run when the camera slowly lerps toward the player when the game scripts. Here is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMotor : MonoBehaviour {
private Vector3 ofset;
private Vector3 characterposition;
private float transition =0.0f;
private float animationduration = 2.0f;
private Vector3 animationoffset = new Vector3(0,2f,2f);
void Start () {
ofset = GameObject.FindObjectOfType<CharacterController> ().transform.position - this.transform.position;
}
// Update is called once per frame
void Update () {
characterposition = GameObject.FindObjectOfType<CharacterController> ().transform.position - ofset;
characterposition.x = 1.3f;
characterposition.y = Mathf.Clamp (characterposition.y, 3f, 5f);
if (transition > 1.0f) {
this.transform.position = characterposition;
} else {
this.transform.position = Vector3.Lerp (characterposition + animationoffset, characterposition, transition);
transition = Time.deltaTime* 1 / animationduration;
this.transform.LookAt (characterposition, Vector3.up);
}
}
}