Unity 3D Game Custom Camera Script (By Me!)

**Hello everyone,

I created this camera that will rotate 360 degrees around your character based off of arrow presses and about 45 degrees up and down… Rotates around in a globe fashion and follows your play when he moves. It’s not perfect, but looks pretty darn good if you ask me :D**

Critiques and optimizations are more then welcome!

  • Ben
/*
 * This Camera will rotate around your character based on your arrow key presses.
 */

public class CameraHandler : MonoBehaviour
{
    // Editable options and used for debug, can be hidden once finished.
    [SerializeField] float currentHorizontalRotation = 0f; // Horizontal Rotation 0 - 360 degrees (Not clamped).
    [SerializeField] float currentVerticalRotation = 0f; // Vertical rotation 2 - 9 degrees (Clamped)
    [SerializeField] float horizontalRotationAmount = 0.035f; // How fast to rotate on horizontal plane.
    [SerializeField] float verticalRotationAmount = 0.35f; // How fast to rotat on vertical plane.
    [SerializeField] bool scrollLeft = false; // Left Arrow, but can be configured for A
    [SerializeField] bool scrollRight = false; // Right Arrow, but can be configured for D
    [SerializeField] bool scrollUp = false; // Up Arrow, but can be configured for W
    [SerializeField] bool scrollDown = false; // Down Arrow, but can be configured for S
    [SerializeField] float cameraDistance = 5f; // Distance of camera from player


    // Instance References
    Transform transform; // Somethings not happy with this.
    GameObject player; // Instance of Player for easy reference.


    // Start is called before the first frame update
    void Start()
    {
        // Shortcuts the current transform and Player.
        transform = GetComponent<Transform>();
        player = GameObject.FindGameObjectWithTag("Player");
        
        // Finds the current horizontal axis and sets ang accordingly, not perfect, bit of a snap.
        float d_x = transform.position.x - player.transform.position.x;
        float d_y = transform.position.y - player.transform.position.y;
        currentHorizontalRotation = Mathf.Atan2(d_y, d_x);

        // Finds the current vertical axis (Z), this one seems to be fine.
        d_x = transform.position.x - player.transform.position.x;
        d_y = transform.position.y - player.transform.position.y;
        currentVerticalRotation = Mathf.Atan2(d_x, d_y);
    }

    // Updates the camera position based off of input.
    void Update()
    {
        // Triggers button down booleans
        if (Input.GetKeyDown(KeyCode.LeftArrow)) scrollLeft = true;
        if (Input.GetKeyDown(KeyCode.RightArrow)) scrollRight = true;
        if (Input.GetKeyDown(KeyCode.UpArrow)) scrollUp = true;
        if (Input.GetKeyDown(KeyCode.DownArrow)) scrollDown = true;

        // De-triggers button down booleans (Released)
        if (Input.GetKeyUp(KeyCode.LeftArrow)) scrollLeft = false;
        if (Input.GetKeyUp(KeyCode.RightArrow)) scrollRight = false;
        if (Input.GetKeyUp(KeyCode.UpArrow)) scrollUp = false;
        if (Input.GetKeyUp(KeyCode.DownArrow)) scrollDown = false;
        
        // Finds currently the pressed velocity horizontal direction. (see what Rick implemented in previous tutorial)
        int horizontalKeyPressedVelocity = 0;
        if (scrollLeft) horizontalKeyPressedVelocity = -1;
        if (scrollRight) horizontalKeyPressedVelocity = 1;
        
        // Finds currently the pressed velocity vertical direction. (see what Rick implemented in previous tutorial)
        int verticalKeyPressedVelocity = 0;
        if (scrollDown) verticalKeyPressedVelocity = -1;
        if (scrollUp)  verticalKeyPressedVelocity = 1;
        
        // Choose a victor, you can't scroll each direction at the same time!
        if (scrollLeft && scrollRight) horizontalKeyPressedVelocity = 0;
        if (scrollUp && scrollDown) verticalKeyPressedVelocity = 0;

        // Calculate the current horicontal scroll rotation 0 - 360 (although I'm sure I need to capture this better)
        currentHorizontalRotation = currentHorizontalRotation + (horizontalRotationAmount * horizontalKeyPressedVelocity);

        // Sets the X and Y position based off of horizontal scroll and angle. Love math.
        float p_x = player.transform.position.x + (Mathf.Cos(currentHorizontalRotation) * cameraDistance);
        float p_y = player.transform.position.z + (Mathf.Sin(currentHorizontalRotation) * cameraDistance);
        transform.position = new Vector3(p_x, transform.position.y, p_y);

        // Sets the Z position based off a clamp version of Z axis, I don't want the character looking from under ground or directly above.
        currentVerticalRotation = Mathf.Clamp(currentVerticalRotation + (verticalRotationAmount * verticalKeyPressedVelocity), 0f, 9f);
        currentVerticalRotation = Mathf.Clamp(currentVerticalRotation, 2f, 9f);
        transform.position = new Vector3(transform.position.x, currentVerticalRotation, transform.position.z);

        // This is nifty! Look at the player. (after movement)
        transform.LookAt(player.transform);
    }
}

Looking at this now, I see a big Rick and Ben “no no”, variables with the name of d_x /d_y and p_x/p_y :grimacing:

Privacy & Terms