- Home /
Why does my camera stop following the player when I add a premade map?
Whenever I add the map, the camera stops following the player, and stays in one position. The player moves, but the camera doesn't. Here is my code for the Camera:
using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace MA { public class CameraHandler : MonoBehaviour { public Transform targetTransform; public Transform cameraTransform; public Transform cameraPivotTransform; private Transform myTransform; private Vector3 cameraTransformPosition; private LayerMask ignoreLayers; 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;
private float defaultPosition;
private float lookAngle;
private float pivotAngle;
public float minimumPivot = -35;
public float maximumPivot = 35;
public float cameraSphereRadius = 0.2f;
public float cameraCollisionOffSet = 0.2f;
public float minimumCollisionOffset = 0.2f;
private void Awake()
{
singleton = this;
myTransform = transform;
defaultPosition = cameraTransform.localPosition.z;
ignoreLayers = ~(1 << 8 | 1 << 9 | 1 << 10);
}
public void FollowTarget(float delta)
{
Vector3 targetPosition = Vector3.SmoothDamp(myTransform.position, targetTransform.position, ref cameraFollowVelocity, delta / followSpeed);
myTransform.position = targetPosition;
HandleCameraCollisions(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;
}
private void HandleCameraCollisions(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)//redo (maybe)
, ignoreLayers))
{
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;
}
}
}
What do you mean by "loading new map"? Do you mean new scene? If so then you will lose any reference to the targetTransform because loading new scene basically destroys any scene object instances that were in the previous scene. If your target is the player then you need to find it and set it's reference in the new scene again.
Your answer
Follow this Question
Related Questions
Normal map color change problem. BUG? 1 Answer
Having some issues with procedurally generating a map 1 Answer
Assigning UV Map to model at runtime 0 Answers
Map disappears after couple seconds 0 Answers
Applying force to a rigidbody 2 Answers