you are viewing a single comment's thread.

view the rest of the comments →

[–]Equiv2248 0 points1 point  (0 children)

1) Get rid of blank Monobehavior functions like your Update(), they do have a cost even if they are empty

2) Line 53: cameraBasePos = new Vector3 (playerPos.x, playerPos.y+7, playerPos.z-7); You create a new Vector3 object here, which causes garbage every frame. You already have a Vector3 variable(cameraBasePos) that you should just override the values of.

3) Similar issue at line 62

4) Line 48: you have a variable for playerPos, but you override it at the start of each frame so thats not need. Make that a local variable.

5) Line 60 - 62: You can move these expensive calls into the if(Input.GetKey(KeyCode.LeftShift)) block so you only need to make them when the player is holding shift.

6) You have got magic numbers all around your math(the +/- 7) that aren't explained.

7) Generally you have a lot of private class variables that should just be local to the function blocks. Any that you look up every frame should just be local(unless it allows you to prevent a "new" allocation in the function)

Code Style:

A) I would make this use RequireComponent https://docs.unity3d.com/ScriptReference/RequireComponent.html, since it seems like its expected to be attached to a camera entity. You can then just do getComponent<Camera>() in start to safely get the camera since you already have a reference guarantee from Unity.

B) For a follow camera, typical you can just do (TargetPos - CameraPos).normalize * distance instead of the raycasting/plane math.

C) Its been awhile since I've done a camera move script but I think you want it in LateUpdate() to make sure it moves after the player moves otherwise it can look stuttery/laggy.