The script is attached to Main Camera.
In the inspector i dragged to the script to target a Cylinder from the Hierarchy.
The camera does rotate and when i’m using a break point on the line number 19:
_direction = (target.position - transform.position).normalized;
target is not null. And it does the rotation.
So why i’m getting this exception ?
This is the exception message:
UnassignedReferenceException: The variable target of LookAtCamera has not been assigned.
You probably need to assign the target variable of the LookAtCamera script in the inspector.
UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:26)
LookAtCamera.Update () (at Assets/MyScripts/LookAtCamera.cs:19)
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtCamera : MonoBehaviour {
//values that will be set in the Inspector
public Transform target;
public float RotationSpeed;
//values for internal use
private Quaternion _lookRotation;
private Vector3 _direction;
// Update is called once per frame
void Update()
{
//find the vector pointing from our position to the target
_direction = (target.position - transform.position).normalized;
//create the rotation we need to be in to look at the target
_lookRotation = Quaternion.LookRotation(_direction);
//rotate us over time according to speed until we are in the required rotation
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
}
}