- Home /
Smooth Camera transition (linear interpolation) Error
Hey guys
sorry if this is a really basic question. So i got this C# script to smoothly transition between camera positions through key-Inputs (see below) with linear interpolation (the different 'views' are empty game objects where the camera should go and the script is attached to the camera.
But now when i try to run it, there is a NullReferenceException ('Object reference not set to an instance of an object')..
Problem is I can't really tell whats wrong with my script (see below)..
Any suggestions where the problem could be?
Many thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl: MonoBehaviour {
public Transform [] views;
public float transitionSpeed;
Transform currentView;
//initialisation
void Start ()
{
}
void Update() {
if (Input.GetKeyDown ("q")) {
currentView = views [0];
}
if (Input.GetKeyDown ("e")) {
currentView = views [1];
}
if (Input.GetKeyDown ("r")) {
currentView = views [2];
}
}
void LateUpdate () {
//Lerp position
transform.position = Vector3.Lerp(transform.position, currentView.position, Time.deltaTime * transitionSpeed);
//get the angles as well
Vector3 currentAngle = new Vector3 (
Mathf.LerpAngle (transform.rotation.eulerAngles.x, currentView.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle (transform.rotation.eulerAngles.y, currentView.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle (transform.rotation.eulerAngles.z, currentView.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSpeed));
transform.eulerAngles = currentAngle;
}
}
Answer by mikelortega · May 30, 2017 at 10:04 AM
The views[] array is probably empty. Make sure it has at least three values before you run.
On the other hand, Time.deltaTime returns the time it took to draw last frame, so when you multiply it with transitionSpeed it will give you the same value and the Lerp won't move your object's position.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Prevent camera from following Touch input position 0 Answers
Distribute terrain in zones 3 Answers
Why does this IF statement loop without finishing? 0 Answers