update
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Invector.vCharacterController;
|
||||
using Invector;
|
||||
using Invector.vCharacterController;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using OnlyScove.Scripts;
|
||||
|
||||
namespace Invector.vCamera
|
||||
{
|
||||
@@ -28,6 +30,19 @@ namespace Invector.vCamera
|
||||
#region inspector properties
|
||||
|
||||
public Transform mainTarget;
|
||||
public InputReader inputReader;
|
||||
|
||||
[Header("First Person View")]
|
||||
public Transform fpvTarget;
|
||||
[SerializeField] protected float fpvFOV = 80f;
|
||||
[SerializeField] protected float transitionDuration = 0.3f;
|
||||
|
||||
[Header("Modular Features")]
|
||||
[SerializeField] protected CameraCharacterFading characterFading = new CameraCharacterFading();
|
||||
[SerializeField] protected CameraOcclusionTransparency occlusionTransparency = new CameraOcclusionTransparency();
|
||||
[SerializeField] protected CameraDynamicFOV dynamicFOV = new CameraDynamicFOV();
|
||||
[SerializeField] protected CameraShakeManager shakeManager = new CameraShakeManager();
|
||||
|
||||
[Tooltip("Lerp speed between Camera States")]
|
||||
[SerializeField] protected float _smoothBetweenState = 6f;
|
||||
public virtual float smoothBetweenState { get { return _smoothBetweenState; } set { _smoothBetweenState = value; } }
|
||||
@@ -195,10 +210,56 @@ namespace Invector.vCamera
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
|
||||
Init();
|
||||
}
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (inputReader != null)
|
||||
{
|
||||
inputReader.OnToggleViewEvent += ToggleCameraView;
|
||||
inputReader.OnSwitchSideEvent += ToggleSwitchSide;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
if (inputReader != null)
|
||||
{
|
||||
inputReader.OnToggleViewEvent -= ToggleCameraView;
|
||||
inputReader.OnSwitchSideEvent -= ToggleSwitchSide;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ToggleSwitchSide()
|
||||
{
|
||||
SwitchRight(switchRight > 0);
|
||||
}
|
||||
|
||||
public virtual void ToggleCameraView()
|
||||
{
|
||||
if (currentState.cameraMode == TPCameraMode.FirstPerson)
|
||||
{
|
||||
ChangeState("Default");
|
||||
}
|
||||
else
|
||||
{
|
||||
ChangeState("FirstPerson");
|
||||
// If the state "FirstPerson" does not exist in the list,
|
||||
// we force the mode to FirstPerson anyway
|
||||
if (currentStateName != "FirstPerson")
|
||||
{
|
||||
currentState.cameraMode = TPCameraMode.FirstPerson;
|
||||
currentStateName = "FirstPerson";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void TriggerFallImpactShake(float fallHeight)
|
||||
{
|
||||
shakeManager.TriggerFallImpactShake(fallHeight);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Init camera.
|
||||
/// </summary>
|
||||
@@ -209,6 +270,9 @@ namespace Invector.vCamera
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputReader == null) inputReader = mainTarget.GetComponent<InputReader>();
|
||||
if (inputReader == null) inputReader = GameObject.FindObjectOfType<InputReader>();
|
||||
|
||||
firstUpdated = true;
|
||||
useSmooth = true;
|
||||
targetLookAt.rotation = startUsingTargetRotation ? mainTarget.rotation : transform.rotation;
|
||||
@@ -256,6 +320,8 @@ namespace Invector.vCamera
|
||||
currentTargetPos = new Vector3(currentTarget.position.x, currentTarget.position.y + offSetPlayerPivot, currentTarget.position.z) + currentTarget.transform.up * lerpState.height;
|
||||
targetLookAt.position = currentTargetPos;
|
||||
|
||||
dynamicFOV.Initialize(currentState.fov, fpvFOV);
|
||||
|
||||
isInit = true;
|
||||
}
|
||||
|
||||
@@ -292,7 +358,19 @@ namespace Invector.vCamera
|
||||
case TPCameraMode.FixedPoint:
|
||||
CameraFixed();
|
||||
break;
|
||||
case TPCameraMode.FirstPerson:
|
||||
CameraMovement();
|
||||
break;
|
||||
}
|
||||
|
||||
// Modular Features
|
||||
if (inputReader != null)
|
||||
{
|
||||
dynamicFOV.HandleDynamicFOV(targetCamera, inputReader,
|
||||
currentState.cameraMode == TPCameraMode.FirstPerson ? CameraController.CameraViewMode.FirstPerson : CameraController.CameraViewMode.ThirdPerson);
|
||||
}
|
||||
shakeManager.HandleShake();
|
||||
transform.position += shakeManager.ShakeOffset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -799,6 +877,12 @@ namespace Invector.vCamera
|
||||
currentState.CopyState(lerpState);
|
||||
}
|
||||
|
||||
if (currentState.cameraMode == TPCameraMode.FirstPerson)
|
||||
{
|
||||
HandleFirstPersonMovement();
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentState.useZoom)
|
||||
{
|
||||
currentZoom = Mathf.Clamp(currentZoom, currentState.minDistance, currentState.maxDistance);
|
||||
@@ -918,9 +1002,44 @@ namespace Invector.vCamera
|
||||
_euler.z = 0;
|
||||
var _rot = Quaternion.Euler(_euler + currentState.rotationOffSet);
|
||||
selfRigidbody.MoveRotation(Quaternion.Lerp(startRotation, _rot, transformWeight));
|
||||
|
||||
// Apply Fading and Transparency
|
||||
characterFading.HandleCharacterFading(distance);
|
||||
occlusionTransparency.HandleTransparency(transform, targetPos);
|
||||
|
||||
movementSpeed = Vector2.zero;
|
||||
}
|
||||
|
||||
protected virtual void HandleFirstPersonMovement()
|
||||
{
|
||||
if (fpvTarget == null) return;
|
||||
|
||||
distance = 0;
|
||||
targetCamera.fieldOfView = fpvFOV;
|
||||
|
||||
// Position
|
||||
current_cPos = fpvTarget.position;
|
||||
targetLookAt.position = current_cPos;
|
||||
selfRigidbody.MovePosition(current_cPos);
|
||||
|
||||
// Rotation
|
||||
float _mouseY = Mathf.LerpAngle(mouseYStart, mouseY, transformWeight);
|
||||
float _mouseX = Mathf.LerpAngle(mouseXStart, mouseX, transformWeight);
|
||||
Quaternion newRot = Quaternion.Euler(_mouseY + offsetMouse.y, _mouseX + offsetMouse.x, 0);
|
||||
targetLookAt.rotation = useSmooth ? Quaternion.Lerp(targetLookAt.rotation, newRot, smoothCameraRotation * Time.fixedDeltaTime) : newRot;
|
||||
|
||||
selfRigidbody.MoveRotation(targetLookAt.rotation);
|
||||
|
||||
// Sync player body rotation (Yaw only)
|
||||
if (mainTarget != null)
|
||||
{
|
||||
mainTarget.rotation = Quaternion.Euler(0, targetLookAt.eulerAngles.y, 0);
|
||||
}
|
||||
|
||||
characterFading.HandleCharacterFading(0); // Fully hide character head if desired, or set to distance 0 for full hidden
|
||||
occlusionTransparency.HandleTransparency(transform, fpvTarget.position);
|
||||
}
|
||||
|
||||
protected virtual void CameraFixed()
|
||||
{
|
||||
if (useSmooth)
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Invector
|
||||
{
|
||||
public enum TPCameraMode
|
||||
{
|
||||
FreeDirectional,
|
||||
FixedAngle,
|
||||
FixedPoint,
|
||||
FirstPerson
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class vThirdPersonCameraState
|
||||
{
|
||||
@@ -56,6 +64,60 @@ namespace Invector
|
||||
fixedAngle = Vector2.zero;
|
||||
cameraMode = TPCameraMode.FreeDirectional;
|
||||
}
|
||||
|
||||
public void CopyState(vThirdPersonCameraState state)
|
||||
{
|
||||
this.Name = state.Name;
|
||||
this.forward = state.forward;
|
||||
this.right = state.right;
|
||||
this.defaultDistance = state.defaultDistance;
|
||||
this.maxDistance = state.maxDistance;
|
||||
this.minDistance = state.minDistance;
|
||||
this.height = state.height;
|
||||
this.smooth = state.smooth;
|
||||
this.smoothDamp = state.smoothDamp;
|
||||
this.xMouseSensitivity = state.xMouseSensitivity;
|
||||
this.yMouseSensitivity = state.yMouseSensitivity;
|
||||
this.yMinLimit = state.yMinLimit;
|
||||
this.yMaxLimit = state.yMaxLimit;
|
||||
this.xMinLimit = state.xMinLimit;
|
||||
this.xMaxLimit = state.xMaxLimit;
|
||||
this.rotationOffSet = state.rotationOffSet;
|
||||
this.cullingHeight = state.cullingHeight;
|
||||
this.cullingMinDist = state.cullingMinDist;
|
||||
this.fov = state.fov;
|
||||
this.useZoom = state.useZoom;
|
||||
this.fixedAngle = state.fixedAngle;
|
||||
this.lookPoints = state.lookPoints;
|
||||
this.cameraMode = state.cameraMode;
|
||||
}
|
||||
|
||||
public void Slerp(vThirdPersonCameraState state, float t)
|
||||
{
|
||||
this.Name = state.Name;
|
||||
this.forward = Mathf.Lerp(this.forward, state.forward, t);
|
||||
this.right = Mathf.Lerp(this.right, state.right, t);
|
||||
this.defaultDistance = Mathf.Lerp(this.defaultDistance, state.defaultDistance, t);
|
||||
this.maxDistance = Mathf.Lerp(this.maxDistance, state.maxDistance, t);
|
||||
this.minDistance = Mathf.Lerp(this.minDistance, state.minDistance, t);
|
||||
this.height = Mathf.Lerp(this.height, state.height, t);
|
||||
this.smooth = Mathf.Lerp(this.smooth, state.smooth, t);
|
||||
this.smoothDamp = Mathf.Lerp(this.smoothDamp, state.smoothDamp, t);
|
||||
this.xMouseSensitivity = Mathf.Lerp(this.xMouseSensitivity, state.xMouseSensitivity, t);
|
||||
this.yMouseSensitivity = Mathf.Lerp(this.yMouseSensitivity, state.yMouseSensitivity, t);
|
||||
this.yMinLimit = Mathf.Lerp(this.yMinLimit, state.yMinLimit, t);
|
||||
this.yMaxLimit = Mathf.Lerp(this.yMaxLimit, state.yMaxLimit, t);
|
||||
this.xMinLimit = Mathf.Lerp(this.xMinLimit, state.xMinLimit, t);
|
||||
this.xMaxLimit = Mathf.Lerp(this.xMaxLimit, state.xMaxLimit, t);
|
||||
this.rotationOffSet = Vector3.Lerp(this.rotationOffSet, state.rotationOffSet, t);
|
||||
this.cullingHeight = Mathf.Lerp(this.cullingHeight, state.cullingHeight, t);
|
||||
this.cullingMinDist = Mathf.Lerp(this.cullingMinDist, state.cullingMinDist, t);
|
||||
this.fov = Mathf.Lerp(this.fov, state.fov, t);
|
||||
this.useZoom = state.useZoom;
|
||||
this.fixedAngle = Vector2.Lerp(this.fixedAngle, state.fixedAngle, t);
|
||||
this.lookPoints = state.lookPoints;
|
||||
this.cameraMode = state.cameraMode;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
@@ -66,11 +128,4 @@ namespace Invector
|
||||
public Vector3 eulerAngle;
|
||||
public bool freeRotation;
|
||||
}
|
||||
|
||||
public enum TPCameraMode
|
||||
{
|
||||
FreeDirectional,
|
||||
FixedAngle,
|
||||
FixedPoint
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user