- Home /
When switching scenes some of my scripts stop working
So I have a camera follow script that I use in my main game scene. When I go from my main menu to that scene the script doesn't work. Even when I start Unity up on just that scene it doesn't work.
The only fix right now is to remove the camera follow script component and add it back on. But obviously that won't work at runtime.
I've looked around my scene and all my other scripts seem to be working fine except that one! Any help with this strange bug would be greatly appreciated!
And In cause you need the code to the camera follow script here it is: using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace AL
{
public class CameraHandler : MonoBehaviour
{
public Transform targetTransform;
public Transform cameraTransform;
public Transform cameraPivotTransform;
private Transform myTransform;
private Vector3 cameraTransformPosition;
private LayerMask ignorLayers;
private Vector3 cameraFollowVelocity = Vector3.zero;
public static CameraHandler singleton;
public float lookSpeed = 0.1f;
public float followSpeed = 0.1f;
public float pivotSpeed = 0.03f;
private float targetPosition;
public float defaultPosition;
private float lookAngle;
private float pivotAngle;
private float minimumPivot = -35f;
private float maximumPivot = 35f;
public float cameraSphereRadius = .2f;
public float cameraCollisionOffset = .2f;
public float minimumCollisionOffset = .2f;
private void Awake()
{
singleton = this;
myTransform = transform;
defaultPosition = cameraTransform.position.z;
ignorLayers = ~(1 << 8 | 1 << 9 | 1 << 10);
}
public void FollowTarget(float delta)
{
if (targetTransform != null)
{
Vector3 targetPosition = Vector3.SmoothDamp(myTransform.position, targetTransform.position, ref cameraFollowVelocity, delta / followSpeed);
myTransform.position = targetPosition;
}
HandleCameraCollision(delta);
}
public void HandleCameraRotation(float delta, float mouseXInput, float mouseYInput)
{
lookAngle += (mouseXInput * lookSpeed) / delta;
pivotAngle += (mouseYInput * pivotSpeed) / delta;
pivotAngle = Mathf.Clamp(pivotAngle, minimumPivot, maximumPivot);
Vector3 rotation = Vector3.zero;
rotation.y = lookAngle;
Quaternion targetRotation = Quaternion.Euler(rotation);
myTransform.rotation = targetRotation;
rotation = Vector3.zero;
rotation.x = pivotAngle;
targetRotation = Quaternion.Euler(rotation);
cameraPivotTransform.localRotation = targetRotation;
}
public void HandleCameraCollision(float delta)
{
targetPosition = defaultPosition;
RaycastHit hit;
Vector3 direction = cameraTransform.position - cameraPivotTransform.position;
direction.Normalize();
if (Physics.SphereCast(
cameraPivotTransform.position,
cameraSphereRadius,
direction, out hit,Mathf.Abs(targetPosition),
ignorLayers))
{
float dis = Vector3.Distance(cameraPivotTransform.position, hit.point);
targetPosition = -(dis - cameraCollisionOffset);
}
if (Mathf.Abs(targetPosition) < minimumCollisionOffset)
{
targetPosition = -minimumCollisionOffset;
}
cameraTransformPosition.z = Mathf.Lerp(cameraTransform.localPosition.z, targetPosition, delta / 0.2f);
cameraTransform.localPosition = cameraTransformPosition;
}
}
}
If you need to know anything else please ask!
Your answer
Follow this Question
Related Questions
How can I instantiate a prefab/s in all loaded scenes or in selected scenes from list ? 1 Answer
How to run script in editor 1 Answer
Bug With My Level Select Screen 2 Answers
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers
Editor calling a constructor for no apparent reason 1 Answer