Bicycle Camera Code Example


using UnityEngine;
using System.Collections;

namespace SBPScripts
{
    public class BicycleCamera : MonoBehaviour
    {
        public Transform target;
        public bool stuntCamera;
        public float distance = 20.0f;
        public float height = 5.0f;
        public float heightDamping = 2.0f;
        public float lookAtHeight = 0.0f;
        public float rotationSnapTime = 0.3F;
        private Vector3 lookAtVector;
        private float usedDistance;
        float wantedRotationAngle;
        float wantedHeight;
        float currentRotationAngle;
        float currentHeight;
        Vector3 wantedPosition;
        private float yVelocity = 0.0F;
        private float zVelocity = 0.0F;
        PerfectMouseLook perfectMouseLook;

        void Start()
        {
            perfectMouseLook = GetComponent();
            if (stuntCamera)
            {
                var follow = new GameObject("Follow");
                var toFollow = GameObject.FindObjectOfType().transform;
                follow.transform.SetParent(toFollow);
                follow.transform.position = toFollow.position + toFollow.gameObject.GetComponent().center;
                target = follow.transform;
                height -= toFollow.gameObject.GetComponent().center.y;
                lookAtHeight -= toFollow.gameObject.GetComponent().center.y;
            }
            else
                target = GameObject.FindObjectOfType().transform;
        }

        void LateUpdate()
        {
            wantedHeight = target.position.y + height;
            currentHeight = transform.position.y;
            wantedRotationAngle = target.eulerAngles.y;
            currentRotationAngle = transform.eulerAngles.y;
            if (perfectMouseLook.movement == false)
                currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.fixedDeltaTime);
            wantedPosition = target.position;
            wantedPosition.y = currentHeight;
            usedDistance = Mathf.SmoothDampAngle(usedDistance, distance, ref zVelocity, 0.1f);
            wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
            transform.position = wantedPosition;
            transform.LookAt(target.position + lookAtVector);
            lookAtVector = new Vector3(0, lookAtHeight, 0);
        }
    }
}
    
    
    

Perfect Mouse Look Code Example


using UnityEngine;
using System.Collections;

namespace SBPScripts
{
    public class PerfectMouseLook : MonoBehaviour
    {
        Vector2 _mouseAbsolute;
        Vector2 _smoothMouse;
        public Vector2 clampInDegrees = new Vector2(360, 180);
        public Vector2 sensitivity = new Vector2(2, 2);
        public Vector2 smoothing = new Vector2(3, 3);
        public Vector2 targetDirection;
        public Vector2 targetCharacterDirection;
        [HideInInspector]
        public bool movement;
        public bool autoRotate;

        void Start()
        {
            targetDirection = transform.localRotation.eulerAngles;
        }

        void LateUpdate()
        {
            var targetOrientation = Quaternion.Euler(targetDirection);
            var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
            var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
            mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
            _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
            _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
            _mouseAbsolute += _smoothMouse;
            if (_smoothMouse == new Vector2(0, 0) && autoRotate)
            {
                targetDirection = transform.localRotation.eulerAngles;
                _mouseAbsolute = new Vector2(0, 0);
                movement = false;
            }
            else
            {
                movement = true;
                if (clampInDegrees.x < 360)
                    _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
                if (clampInDegrees.y < 360)
                    _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
                transform.localRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right) * targetOrientation;
                var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
                transform.localRotation *= yRotation;
            }
        }
    }
}