Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Mirror;
- namespace DM
- {
- public class CameraManager : NetworkBehaviour
- {
- public float followSpeed = .0001f; //defines camera speed that follows character.
- public float mouseSpeed = .0001f; //defines mouse's control speed.
- public float controllerSpeed = .0001f; //defines joypad's control speed.
- public Transform target; //stores the target for camera.
- [HideInInspector]
- public Transform pivot; //pivot for camera's rotation.
- [HideInInspector]
- public Transform camTrans; //stores camera's root transform.
- float turnSmoothing = 1f; //defines how smooth this camera moves.
- public float minAngle = -45; //minimum vertical angle.
- public float maxAngle = 45f; //maximum vertical angle.
- public float minLook = -45; //minimum vertical angle.
- public float maxLook = 45f; //maximum vertical angle.
- float smoothX;
- float smoothY;
- float smoothXVelocity;
- float smoothYVelocity;
- public float lookAngle;
- public float tiltAngle;
- float h;
- float v;
- public void Update()
- {
- target = this.transform.parent;
- }
- public void Init(Transform t) //Initiallize camera settings.
- {
- target = t;
- camTrans = Camera.main.transform;
- pivot = camTrans.parent;
- }
- public void FixedTick(float d) //Getting camera inputs and updates camera's stats.
- {
- if (isLocalPlayer) {
- h = Input.GetAxis("Mouse X");
- v = Input.GetAxis("Mouse Y");
- }
- //float c_h = Input.GetAxis("RightAxis X");
- //float c_v = Input.GetAxis("RightAxis Y");
- float targetSpeed = mouseSpeed;
- //if(c_h != 0 || c_v != 0)
- //{
- //h = c_h;
- //v = c_v;
- //targetSpeed = controllerSpeed;
- //}
- FollowTarget(d);
- HandleRotations(d, v, h, targetSpeed);
- }
- void FollowTarget(float d) //defines how camera follows the target.
- {
- float speed = d * followSpeed;
- Vector3 targetPosition = Vector3.Lerp(transform.position, target.position, speed);
- targetPosition.y += 0.5F;
- targetPosition.z += 0.5F;
- transform.position = targetPosition;
- }
- void HandleRotations(float d, float v, float h, float targetSpeed) //defines the rotation of camera.
- {
- if(turnSmoothing > 0)
- {
- smoothX = Mathf.SmoothDamp(smoothX, h, ref smoothXVelocity, turnSmoothing);
- smoothY = Mathf.SmoothDamp(smoothY, v, ref smoothYVelocity, turnSmoothing);
- }
- else
- {
- smoothX = h;
- smoothY = v;
- }
- lookAngle += smoothX * targetSpeed;
- // lookAngle = Mathf.Clamp(lookAngle, minLook, maxLook);
- transform.rotation = Quaternion.Euler(0, lookAngle, 0);
- tiltAngle -= smoothY * targetSpeed;
- tiltAngle = Mathf.Clamp(tiltAngle, minAngle, maxAngle);
- pivot.localRotation = Quaternion.Euler(tiltAngle, 0, 0);
- }
- public static CameraManager singleton;
- private void Awake()
- {
- singleton = this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement