Guest User

Untitled

a guest
May 9th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerCamera : MonoBehaviour
  6. {
  7.  
  8.     // The target we are following
  9.     public GameObject targetGameObject;
  10.     Transform target;
  11.  
  12.     RaycastHit cameraObstacle;
  13.  
  14.     float distance = 2.5f;
  15.     public float initialDistance = 2.5f;
  16.     public float height = 1.5f;
  17.     public float heightDamping = 2.0f;
  18.     public float rotationDamping = 3.0f;
  19.     public float rotationSpeed = 1.0f;
  20.     float heightSpeed = 0.03f;
  21.     public float offsetY = 1.6f;
  22.     public float targToCamDist;
  23.  
  24.     Vector3 localPosition;
  25.     Vector3 saveCamPos;
  26.     Vector3 offSet;
  27.  
  28.     float wantedRotationAngle;
  29.     float wantedHeight;
  30.     float currentRotationAngle;
  31.     float currentHeight;
  32.     float camTransTimer;
  33.     float camTransDuration;
  34.     float intensity = 0, shakeDuration = 0;
  35.     Quaternion currentRotation;
  36.  
  37.     GameObject bedObject;
  38.  
  39.     //Camera clipping
  40.     float currentLerpTime = 0;
  41.     float lerpTimeDuration = 2f;
  42.  
  43.  
  44.     /// <summary>
  45.     /// TODO:
  46.     /// Maybe add cameracollisioncheck
  47.     /// </summary>
  48.  
  49.     public void Initialize()
  50.     {
  51.         target = targetGameObject.transform;
  52.         localPosition = transform.localPosition;
  53.         transform.rotation = target.rotation;
  54.         camTransTimer = 0;
  55.         camTransDuration = 1;
  56.         bedObject = GameObject.FindGameObjectWithTag("Bed");
  57.     }
  58.  
  59.     public void UpdateFollowerCamera(ref CameraData _cameraData)
  60.     {
  61.         // Early out if we don't have a target
  62.         if (!target)
  63.             return;
  64.  
  65.         // Calculate the current rotation angles
  66.         //if (Input.GetAxis("Horizontal_RightStick") != 0 || (targetGameObject.GetComponent<PlayerController>().GetAxis() == Vector2.zero))
  67.             wantedRotationAngle += Input.GetAxis("Horizontal_RightStick") * rotationSpeed;
  68.         //else
  69.         //wantedRotationAngle = target.eulerAngles.y;
  70.  
  71.         wantedHeight = target.position.y + height;
  72.  
  73.         height += Input.GetAxis("Vertical_RightStick") * heightSpeed; //
  74.  
  75.         if (height > 3f)
  76.             height = 3f;
  77.         else if (height < 0.7f)
  78.             height = 0.7f;
  79.  
  80.         currentRotationAngle = transform.eulerAngles.y;
  81.         currentHeight = transform.position.y;
  82.  
  83.         // Damp the rotation around the y-axis
  84.         currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
  85.  
  86.         // Damp the height
  87.         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
  88.  
  89.         // Convert the angle into a rotation
  90.         currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
  91.  
  92.         // Set the position of the camera on the x-z plane to:
  93.         // distance meters behind the target
  94.         transform.position = target.position;
  95.         transform.position -= currentRotation * Vector3.forward * distance;
  96.  
  97.         // Set the height of the camera
  98.         Vector3 newPos = transform.position;
  99.         newPos.y = currentHeight;
  100.         transform.position = newPos;
  101.  
  102.         transform.localPosition = newPos + CameraShake(intensity);
  103.  
  104.         // Always look at the target
  105.         Vector3 tempTarget = target.position;
  106.         tempTarget.y += offsetY;
  107.  
  108.         transform.LookAt(tempTarget, target.up);
  109.  
  110.         //Update camera data
  111.         _cameraData.SetCamPos(transform.position);
  112.         _cameraData.SetCamRot(transform.rotation);
  113.  
  114.         saveCamPos = transform.position;
  115.  
  116.         CameraClipping();
  117.  
  118.  
  119.  
  120.     }
  121.  
  122.     public void LerpToPositionStatic(ref CameraData _cameraData)
  123.     {
  124.         //Add lerp to/from fixed camera position
  125.         camTransTimer += Time.deltaTime;
  126.         float percentage = camTransTimer / camTransDuration;
  127.  
  128.         transform.position = Vector3.Lerp(_cameraData.GetCamPos(), _cameraData.GetTargetPos(), percentage);
  129.         //transform.rotation = Quaternion.Lerp(_cameraData.GetCamRot(), _cameraData.GetTargetRot(), percentage);
  130.         transform.LookAt(target, target.up);
  131.         if (percentage >= 1) // && transform.rotation == _cameraData.GetTargetRot())
  132.         {
  133.             percentage = 0;
  134.             camTransTimer = 0;
  135.             _cameraData.SetCamState(CameraState.StaticState);
  136.         }
  137.     }
  138.  
  139.     public void LerpToPositionFollow(ref CameraData _cameraData)
  140.     {
  141.         //Add lerp to/from fixed camera position
  142.         camTransTimer += Time.deltaTime;
  143.         float percentage = camTransTimer / camTransDuration;
  144.  
  145.         transform.position = Vector3.Lerp(_cameraData.GetTargetPos(), saveCamPos, percentage);
  146.         //transform.rotation = Quaternion.Lerp(_cameraData.GetCamRot(), _cameraData.GetTargetRot(), percentage);
  147.         transform.LookAt(target, target.up);
  148.         if (percentage >= 1) // && transform.rotation == _cameraData.GetTargetRot())
  149.         {
  150.             percentage = 0;
  151.             camTransTimer = 0;
  152.             _cameraData.SetCamState(CameraState.FollowState);
  153.         }
  154.     }
  155.  
  156.     public void UpdateFixedCamera()
  157.     {
  158.         transform.LookAt(target, target.up);
  159.     }
  160.  
  161.     public void MenuCamera()
  162.     {
  163.         transform.LookAt(bedObject.transform.position);
  164.     }
  165.  
  166.     Vector3 CameraShake(float _intensity)
  167.     {
  168.         if (shakeDuration > 0)
  169.         {
  170.  
  171.             shakeDuration -= Time.deltaTime;
  172.             float randomShakeX = Random.Range(-_intensity, _intensity);
  173.             float randomShakeY = Random.Range(-_intensity, _intensity);
  174.             float randomShakeZ = Random.Range(-_intensity, _intensity);
  175.             offSet = new Vector3(randomShakeX * shakeDuration, randomShakeY * shakeDuration, randomShakeZ * shakeDuration);          //Vector3.Lerp(transform.position + new Vector3(randomShakeX, 0, 0), transform.position + new Vector3(0, randomShakeY, 0), _shakeLength);
  176.         }
  177.         else
  178.         {
  179.             offSet = Vector3.zero;
  180.         }
  181.  
  182.         return offSet;
  183.     }
  184.  
  185.     public void InitCameraShake(float _intensity, float _duration)
  186.     {
  187.         intensity = _intensity;
  188.         shakeDuration = _duration;
  189.     }
  190.  
  191.     void CameraClipping()
  192.     {
  193.         Vector3 rayOrigin = target.transform.position + new Vector3(0, 1.0f,0);
  194.  
  195.         //targToCamDist = Vector3.Distance(target.transform.position, transform.position) - targetGameObject.GetComponent<Collider>().bounds.size.x;
  196.         Vector3 direction = transform.forward;
  197.         direction.y = 0;
  198.         if (Physics.Raycast(rayOrigin, -direction, out cameraObstacle, initialDistance))
  199.         {
  200.             if (cameraObstacle.collider.bounds.size.y > targetGameObject.GetComponent<Collider>().bounds.size.y * 1.5f)
  201.             {
  202.  
  203.                 distance = Vector3.Distance(target.position, cameraObstacle.point) - 0.5f;
  204.             }
  205.             Debug.DrawLine(rayOrigin, cameraObstacle.point, Color.red);
  206.             currentLerpTime = 0;
  207.         }
  208.         else
  209.         {
  210.             if (currentLerpTime < lerpTimeDuration)
  211.             {
  212.                 currentLerpTime += Time.deltaTime;
  213.             }
  214.             else
  215.             {
  216.                 currentLerpTime = lerpTimeDuration;
  217.             }
  218.             float percentage = currentLerpTime / lerpTimeDuration;
  219.             distance = Mathf.Lerp(distance, initialDistance, percentage);
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment