Camera doesn't teleport correctly in build, but does in the editor.

I’m making FNAF in unity, and in the editor, I can die and am jumpscared correctly. However, in the build, I’m looking at his feet. See the video below for more context.

48fpc0

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: Unity Engine - Unity Discussions

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

And if you just have a bug in your current code, then it is … Time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Here’s my code


GameObject.Find("Player").transform.position = (GameObject.Find("JumpscareTPChica").transform.position);
Camera.main.transform.rotation = Quaternion.Euler(0, 0, 0);
GameObject.Find("Player").GetComponent<CharacterController>().enabled = false;
GameObject.Find("Player").GetComponent<PlayerMovement>().moveSpeed = 0;
GameObject.Find("Player").GetComponent<FirstPersonCamera>().xAxisTurnRate = 0;
GameObject.Find("Player").GetComponent<FirstPersonCamera>().yAxisTurnRate = 0;

it works perfectly fine in the editor

Excellent. Get busy debugging, see what you can find out. You can attach the debugger to the built game or you can log it there as well with the runtime logs. Google for how. Staring at code is not how it gets fixed, generally speaking.

Making the jumpscare it’s own scene seems to fix it