Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug CameraHandler

Discussion in 'Scripting' started by TrexMatrix, Sep 3, 2023.

  1. TrexMatrix


    Aug 8, 2023
    I am making a 3rd person rpg but whenever I play the game the camera zooms out really far instead of being where I have it put, how do I fix this?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. namespace HR
    5. {
    6. public class CameraHandler : MonoBehaviour
    7. {
    8. public Transform targetTransform;
    9. public Transform cameraTransform;
    10. public Transform cameraPivotTransform;
    11. private Transform myTransform;
    12. private Vector3 cameraTransformPosition;
    13. private LayerMask ignoreLayers;
    14. private Vector3 cameraFollowVelocity =;
    15. public static CameraHandler singleton;
    16. public float lookSpeed = 0.1f;
    17. public float followSpeed = 0.1f;
    18. public float pivotSpeed = 0.03f;
    19. private float targetPosition;
    20. private float defaultPosition;
    21. private float lookAngle;
    22. private float pivotAngle;
    23. public float minimumPivot = -35;
    24. public float maximumPviot = 35;
    25. public float cameraSphereRadius = 0.2f;
    26. public float cameraCollisionOffSet = 0.2f;
    27. public float minimumCollisionOffSet = 0.2f;
    29. private void Awake()
    30. {
    31. singleton = this;
    32. myTransform = transform;
    33. defaultPosition = cameraTransform.localPosition.z;
    34. ignoreLayers = ~(1 << 8 | 1 << 9 | 1 << 10);
    35. }
    36. public void FollowTarget(float delta)
    37. {
    38. Vector3 targetPosition = Vector3.SmoothDamp(myTransform.position, targetTransform.position, ref cameraFollowVelocity, delta / followSpeed);
    39. myTransform.position = targetPosition;
    40. HandleCameraCollisions(delta);
    41. }
    42. public void HandleCameraRotation(float delta, float mouseXInput, float mouseYInput)
    43. {
    44. lookAngle += (mouseXInput * lookSpeed) / delta;
    45. pivotAngle -= (mouseYInput * pivotSpeed) / delta;
    46. pivotAngle = Mathf.Clamp(pivotAngle, minimumPivot, maximumPviot);
    47. Vector3 rotation =;
    48. rotation.y = lookAngle;
    49. Quaternion targetRotation = Quaternion.Euler(rotation);
    50. myTransform.rotation = targetRotation;
    51. rotation =;
    52. rotation.x = pivotAngle;
    53. targetRotation = Quaternion.Euler(rotation);
    54. cameraPivotTransform.localRotation = targetRotation;
    55. }
    56. private void HandleCameraCollisions(float delta)
    57. {
    58. targetPosition = defaultPosition;
    59. RaycastHit hit;
    60. Vector3 direction = cameraTransform.position - cameraPivotTransform.position;
    61. direction.Normalize();
    62. if (Physics.SphereCast(cameraPivotTransform.position, cameraSphereRadius, direction, out hit, Mathf.Abs(targetPosition), ignoreLayers))
    63. {
    64. float dis = Vector3.Distance(cameraPivotTransform.position, hit.point);
    65. targetPosition = -(dis - cameraCollisionOffSet);
    66. }
    67. if (Mathf.Abs(targetPosition) < minimumCollisionOffSet)
    68. {
    69. targetPosition = -minimumCollisionOffSet;
    70. }
    71. cameraTransformPosition.z = Mathf.Lerp(cameraTransform.localPosition.z, targetPosition, delta / 0.2f);
    72. cameraTransform.localPosition = cameraTransformPosition;
    73. }
    74. }
    75. }
  2. Kurt-Dekker


    Mar 16, 2013
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum:

    If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.

    Otherwise, if you want to debug your code try this method:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

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

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: or this answer for Android:

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
    wideeyenow_unity likes this.
  3. frescoadobe60


    Aug 31, 2023
    i see !!! :)
  4. TrexMatrix


    Aug 8, 2023
    how do i fix it?