Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerCamera : MonoBehaviour
- {
- // The target we are following
- public GameObject targetGameObject;
- Transform target;
- RaycastHit cameraObstacle;
- float distance = 2.5f;
- public float initialDistance = 2.5f;
- public float height = 1.5f;
- public float heightDamping = 2.0f;
- public float rotationDamping = 3.0f;
- public float rotationSpeed = 1.0f;
- float heightSpeed = 0.03f;
- public float offsetY = 1.6f;
- public float targToCamDist;
- Vector3 localPosition;
- Vector3 saveCamPos;
- Vector3 offSet;
- float wantedRotationAngle;
- float wantedHeight;
- float currentRotationAngle;
- float currentHeight;
- float camTransTimer;
- float camTransDuration;
- float intensity = 0, shakeDuration = 0;
- Quaternion currentRotation;
- GameObject bedObject;
- //Camera clipping
- float currentLerpTime = 0;
- float lerpTimeDuration = 2f;
- /// <summary>
- /// TODO:
- /// Maybe add cameracollisioncheck
- /// </summary>
- public void Initialize()
- {
- target = targetGameObject.transform;
- localPosition = transform.localPosition;
- transform.rotation = target.rotation;
- camTransTimer = 0;
- camTransDuration = 1;
- bedObject = GameObject.FindGameObjectWithTag("Bed");
- }
- public void UpdateFollowerCamera(ref CameraData _cameraData)
- {
- // Early out if we don't have a target
- if (!target)
- return;
- // Calculate the current rotation angles
- //if (Input.GetAxis("Horizontal_RightStick") != 0 || (targetGameObject.GetComponent<PlayerController>().GetAxis() == Vector2.zero))
- wantedRotationAngle += Input.GetAxis("Horizontal_RightStick") * rotationSpeed;
- //else
- //wantedRotationAngle = target.eulerAngles.y;
- wantedHeight = target.position.y + height;
- height += Input.GetAxis("Vertical_RightStick") * heightSpeed; //
- if (height > 3f)
- height = 3f;
- else if (height < 0.7f)
- height = 0.7f;
- currentRotationAngle = transform.eulerAngles.y;
- currentHeight = transform.position.y;
- // Damp the rotation around the y-axis
- currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
- // Damp the height
- currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
- // Convert the angle into a rotation
- currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
- // Set the position of the camera on the x-z plane to:
- // distance meters behind the target
- transform.position = target.position;
- transform.position -= currentRotation * Vector3.forward * distance;
- // Set the height of the camera
- Vector3 newPos = transform.position;
- newPos.y = currentHeight;
- transform.position = newPos;
- transform.localPosition = newPos + CameraShake(intensity);
- // Always look at the target
- Vector3 tempTarget = target.position;
- tempTarget.y += offsetY;
- transform.LookAt(tempTarget, target.up);
- //Update camera data
- _cameraData.SetCamPos(transform.position);
- _cameraData.SetCamRot(transform.rotation);
- saveCamPos = transform.position;
- CameraClipping();
- }
- public void LerpToPositionStatic(ref CameraData _cameraData)
- {
- //Add lerp to/from fixed camera position
- camTransTimer += Time.deltaTime;
- float percentage = camTransTimer / camTransDuration;
- transform.position = Vector3.Lerp(_cameraData.GetCamPos(), _cameraData.GetTargetPos(), percentage);
- //transform.rotation = Quaternion.Lerp(_cameraData.GetCamRot(), _cameraData.GetTargetRot(), percentage);
- transform.LookAt(target, target.up);
- if (percentage >= 1) // && transform.rotation == _cameraData.GetTargetRot())
- {
- percentage = 0;
- camTransTimer = 0;
- _cameraData.SetCamState(CameraState.StaticState);
- }
- }
- public void LerpToPositionFollow(ref CameraData _cameraData)
- {
- //Add lerp to/from fixed camera position
- camTransTimer += Time.deltaTime;
- float percentage = camTransTimer / camTransDuration;
- transform.position = Vector3.Lerp(_cameraData.GetTargetPos(), saveCamPos, percentage);
- //transform.rotation = Quaternion.Lerp(_cameraData.GetCamRot(), _cameraData.GetTargetRot(), percentage);
- transform.LookAt(target, target.up);
- if (percentage >= 1) // && transform.rotation == _cameraData.GetTargetRot())
- {
- percentage = 0;
- camTransTimer = 0;
- _cameraData.SetCamState(CameraState.FollowState);
- }
- }
- public void UpdateFixedCamera()
- {
- transform.LookAt(target, target.up);
- }
- public void MenuCamera()
- {
- transform.LookAt(bedObject.transform.position);
- }
- Vector3 CameraShake(float _intensity)
- {
- if (shakeDuration > 0)
- {
- shakeDuration -= Time.deltaTime;
- float randomShakeX = Random.Range(-_intensity, _intensity);
- float randomShakeY = Random.Range(-_intensity, _intensity);
- float randomShakeZ = Random.Range(-_intensity, _intensity);
- 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);
- }
- else
- {
- offSet = Vector3.zero;
- }
- return offSet;
- }
- public void InitCameraShake(float _intensity, float _duration)
- {
- intensity = _intensity;
- shakeDuration = _duration;
- }
- void CameraClipping()
- {
- Vector3 rayOrigin = target.transform.position + new Vector3(0, 1.0f,0);
- //targToCamDist = Vector3.Distance(target.transform.position, transform.position) - targetGameObject.GetComponent<Collider>().bounds.size.x;
- Vector3 direction = transform.forward;
- direction.y = 0;
- if (Physics.Raycast(rayOrigin, -direction, out cameraObstacle, initialDistance))
- {
- if (cameraObstacle.collider.bounds.size.y > targetGameObject.GetComponent<Collider>().bounds.size.y * 1.5f)
- {
- distance = Vector3.Distance(target.position, cameraObstacle.point) - 0.5f;
- }
- Debug.DrawLine(rayOrigin, cameraObstacle.point, Color.red);
- currentLerpTime = 0;
- }
- else
- {
- if (currentLerpTime < lerpTimeDuration)
- {
- currentLerpTime += Time.deltaTime;
- }
- else
- {
- currentLerpTime = lerpTimeDuration;
- }
- float percentage = currentLerpTime / lerpTimeDuration;
- distance = Mathf.Lerp(distance, initialDistance, percentage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment