Created
April 13, 2024 03:13
-
-
Save phosphoer/3e827727dcf0edbef9a9345d7912d510 to your computer and use it in GitHub Desktop.
Camera Mouse Movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class CameraControllerPhoto : CameraControllerBase | |
{ | |
public float Sensitivity = 1.5f; | |
public float MaxPitch = 45.0f; | |
public float MoveSpeed = 10; | |
public float MaxDistance = 30.0f; | |
public float MinDistToGeometry = 2.0f; | |
public float MinDistToTerrain = 5.0f; | |
public float MinDistToWater = 0; | |
[SerializeField] | |
private LayerMask _cameraTerrainMask = default(LayerMask); | |
[SerializeField] | |
private LayerMask _cameraCollideMask = default(LayerMask); | |
private Vector2 _smoothAxis; | |
public override void CameraStart() | |
{ | |
} | |
public override void CameraStop() | |
{ | |
} | |
public override void CameraUpdate() | |
{ | |
float y = AxisX * Sensitivity; | |
float x = AxisY * -Sensitivity; | |
_smoothAxis = Mathfx.Damp(_smoothAxis, new Vector2(x, y), 0.25f, Time.unscaledDeltaTime * 5.0f); | |
// Rotate mount point | |
MountPoint.transform.Rotate(0, _smoothAxis.y, 0, Space.World); | |
MountPoint.transform.Rotate(_smoothAxis.x, 0, 0, Space.Self); | |
// Clamp x rotation | |
Vector3 flatForward = MountPoint.transform.forward.WithY(0); | |
float rotationSign = Mathf.Sign(MountPoint.transform.forward.y); | |
float xRot = Vector3.Angle(flatForward.normalized, MountPoint.transform.forward); | |
float deltaFromMax = Mathf.Max(xRot - MaxPitch, 0); | |
if (deltaFromMax > 0) | |
{ | |
MountPoint.transform.Rotate(deltaFromMax * rotationSign, 0, 0, Space.Self); | |
} | |
Rewired.Player player = PlayerBoatController.MainInstance.RewiredLink.RewiredPlayer; | |
// Camera fly controls | |
bool speedBoost = player.GetButton(RewiredConsts.Action.ToggleGadgetPrimary); | |
float axisX = player.GetAxis(RewiredConsts.Action.CameraHorizontalAxis) * (speedBoost ? 2 : 1); | |
float axisY = player.GetAxis(RewiredConsts.Action.CameraForwardAxis) * (speedBoost ? 2 : 1); | |
Vector3 cameraPos = MountPoint.transform.position; | |
cameraPos += MountPoint.forward * axisY * Time.unscaledDeltaTime * MoveSpeed; | |
cameraPos += MountPoint.right * axisX * Time.unscaledDeltaTime * MoveSpeed; | |
Vector3 boatToCamera = cameraPos - PlayerBoatController.MainInstance.Boat.transform.position; | |
boatToCamera = Vector3.ClampMagnitude(boatToCamera, MaxDistance); | |
Vector3 clampedCameraPos = PlayerBoatController.MainInstance.Boat.transform.position + boatToCamera; | |
cameraPos = Mathfx.Damp(cameraPos, clampedCameraPos, 0.25f, Time.unscaledDeltaTime * 3.0f); | |
// Stop collisions | |
RaycastHit hitInfo; | |
Vector3 cameraOldPos = MountPoint.transform.position; | |
Vector3 cameraMove = cameraPos - cameraOldPos; | |
if (Physics.SphereCast(cameraOldPos, 1.0f, cameraMove.normalized, out hitInfo, MinDistToGeometry + 1, _cameraCollideMask, QueryTriggerInteraction.Ignore)) | |
{ | |
// Find the plane representing the point + normal we hit | |
Plane hitPlane = new Plane(hitInfo.normal, hitInfo.point); | |
// Now project our position onto that plane and use the vector from | |
// the projected point to our pos as the adjusted normal | |
Vector3 closestPoint = hitPlane.ClosestPointOnPlane(cameraPos); | |
Vector3 closestPointToPos = cameraPos - closestPoint; | |
// "Clamp" our distance from the plane to a min distance | |
float planeDist = Vector3.Distance(closestPoint, cameraPos); | |
float adjustedDist = Mathf.Max(planeDist, MinDistToGeometry); | |
cameraPos = closestPoint + closestPointToPos.normalized * adjustedDist; | |
} | |
// Clamp to ground pos | |
if (Physics.Raycast(cameraPos.WithY(50), Vector3.down, out hitInfo, 100, _cameraTerrainMask, QueryTriggerInteraction.Ignore)) | |
{ | |
cameraPos.y = Mathf.Max(cameraPos.y, hitInfo.point.y + MinDistToTerrain); | |
} | |
// Clamp to water pos | |
cameraPos.y = Mathf.Max(cameraPos.y, MinDistToWater); | |
MountPoint.position = cameraPos; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment