- Home /
Question by
ItzChris92 · Jul 09, 2020 at 12:56 PM ·
visual studiocamera follow
[SOLVED] Why won't my camera follow my player?
Hey there, this script worked before I started dynamically spawning my player within the scene. My only change was the adding of a PlayerSpawned event to ensure my player had spawned before the script tried to reference it. But now the camera will not follow the player.
using UnityEngine;
public class CameraFollowPlayer : MonoBehaviour
{
private Transform target;
private Vector3 offset;
private float smoothing = 5.0f;
private void OnEnable()
{
EventManager.playerSpawned += FollowTarget;
}
private void OnDisable()
{
EventManager.playerSpawned -= FollowTarget;
}
private void FollowTarget()
{
target = GameObject.FindWithTag("Player").transform;
offset = transform.position - target.position;
}
private void FixedUpdate()
{
if (target != null)
{
Vector3 targetCameraPosition = target.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCameraPosition, smoothing * Time.deltaTime);
}
}
}
Comment
Best Answer
Answer by ItzChris92 · Jul 09, 2020 at 01:03 PM
Silly mistake by myself. I had copied and edited the PlayerSpawned event within my event manager but was still invoking the copied event. Oops
Your answer
![](https://koobas.hobune.stream/wayback/20220613002824im_/https://answers.unity.com/themes/thub/images/avi.jpg)