Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

Expand script to stop camera from following player

$
0
0
Hi, I'm having a problem figuring out what to do with my script that has the camera follow the player when the player dies. I'm getting the following error: MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineTransform.cs:28) Camera2DFollow.Update () (at Assets/Sample Assets/2D/Scripts/Camera2DFollow.cs:28) Which takes me to the following code snippet: void Update () { // only update lookahead pos if accelerating or changed direction float xMoveDelta = (target.position - lastTargetPosition).x; bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold; if (updateLookAheadTarget) { lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta); } else { lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed); } Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ; Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping); transform.position = newPos; lastTargetPosition = target.position; } } Any suggestions would be greatly appreciated! Thanks!

Viewing all articles
Browse latest Browse all 7934