Add Camera controlled by player. #7 (#10)
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				ci/woodpecker/push/woodpecker Pipeline was successful
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	ci/woodpecker/push/woodpecker Pipeline was successful
				
			Adds the player camera as described in #7. As a note for the future that I forgot to add as a comment here, the camera zoom and rotation should be tweened to give a smoother effect. Reviewed-on: #10 Reviewed-by: zephyr <zephyr@noreply.localhost> Co-authored-by: Michael <mep053@gmail.com> Co-committed-by: Michael <mep053@gmail.com>
This commit is contained in:
		| @@ -1,3 +1,16 @@ | ||||
| { | ||||
| 	"name": "IdleSurvivors" | ||||
| } | ||||
|     "name": "IdleSurvivors", | ||||
|     "rootNamespace": "", | ||||
|     "references": [ | ||||
|         "GUID:75469ad4d38634e559750d17036d5f7c" | ||||
|     ], | ||||
|     "includePlatforms": [], | ||||
|     "excludePlatforms": [], | ||||
|     "allowUnsafeCode": false, | ||||
|     "overrideReferences": false, | ||||
|     "precompiledReferences": [], | ||||
|     "autoReferenced": true, | ||||
|     "defineConstraints": [], | ||||
|     "versionDefines": [], | ||||
|     "noEngineReferences": false | ||||
| } | ||||
							
								
								
									
										3
									
								
								Assets/Scripts/Input.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Assets/Scripts/Input.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: d0cf95adbbea4f6ea1e1d7c250abb38f | ||||
| timeCreated: 1690824944 | ||||
							
								
								
									
										41
									
								
								Assets/Scripts/Input/PlayerCameraInputHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								Assets/Scripts/Input/PlayerCameraInputHandler.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| using DefaultNamespace; | ||||
| using UnityEngine; | ||||
| using UnityEngine.InputSystem; | ||||
| 
 | ||||
| namespace Input | ||||
| { | ||||
|     public class PlayerCameraInputHandler : MonoBehaviour | ||||
|     { | ||||
|         [SerializeField] private PlayerCameraController cameraController; | ||||
|         [SerializeField] [Range(0, 100)] private float rotationSensitivity; | ||||
|         [SerializeField] [Range(0, 1)] private float zoomSensitivity; | ||||
| 
 | ||||
|         private bool _allowRotation; | ||||
|          | ||||
|         public void OnInputZoom(InputAction.CallbackContext context) | ||||
|         { | ||||
|             var input = context.ReadValue<float>(); | ||||
|             var zoomAmount = input * zoomSensitivity * Time.deltaTime; | ||||
|             cameraController.Zoom(zoomAmount); | ||||
|         } | ||||
| 
 | ||||
|         public void OnInputRotate(InputAction.CallbackContext context) | ||||
|         { | ||||
|             if (context.phase != InputActionPhase.Performed) return; | ||||
|             if (!_allowRotation) return; | ||||
|              | ||||
|             var input = context.ReadValue<float>(); | ||||
|             var rotationAmount = input * rotationSensitivity * Time.deltaTime; | ||||
|             cameraController.RotateAround(rotationAmount); | ||||
|         } | ||||
| 
 | ||||
|         public void OnInputHoldToRotate(InputAction.CallbackContext context) | ||||
|         { | ||||
|             _allowRotation = context.phase switch | ||||
|             { | ||||
|                 InputActionPhase.Performed => true, | ||||
|                 _ => false | ||||
|             }; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3
									
								
								Assets/Scripts/Input/PlayerCameraInputHandler.cs.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Assets/Scripts/Input/PlayerCameraInputHandler.cs.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 0b94be55d5ec4fedb531d50297528d3b | ||||
| timeCreated: 1690824962 | ||||
							
								
								
									
										37
									
								
								Assets/Scripts/PlayerCameraController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								Assets/Scripts/PlayerCameraController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| using System; | ||||
| using UnityEngine; | ||||
| using UnityEngine.Serialization; | ||||
| 
 | ||||
| namespace DefaultNamespace | ||||
| { | ||||
|     public class PlayerCameraController : MonoBehaviour | ||||
|     { | ||||
|         [SerializeField] private Transform logicalCamera; | ||||
|         [SerializeField] private Transform physicalCamera; | ||||
|          | ||||
|         [SerializeField] private Transform pivot; | ||||
|         [SerializeField] private Transform minZoomTarget; | ||||
|         [SerializeField] private Transform maxZoomTarget; | ||||
|          | ||||
|         private float _zoomAmount; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// Rotates the logical camera around the pivot by angle degrees. | ||||
|         /// </summary> | ||||
|         /// <param name="angle">The amount to rotate, in degrees</param> | ||||
|         public void RotateAround(float angle) | ||||
|         { | ||||
|             logicalCamera.RotateAround(pivot.position, Vector3.up, angle); | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Lerps the physical camera between the min zoom target and the max zoom target by the current zoom + amount | ||||
|         /// </summary> | ||||
|         /// <param name="amount"></param> | ||||
|         public void Zoom(float amount) | ||||
|         { | ||||
|             _zoomAmount = Mathf.Clamp01(_zoomAmount + amount); | ||||
|             physicalCamera.localPosition = Vector3.Lerp(minZoomTarget.localPosition, maxZoomTarget.localPosition, _zoomAmount); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3
									
								
								Assets/Scripts/PlayerCameraController.cs.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Assets/Scripts/PlayerCameraController.cs.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 58db32f794614503b9c8d13bf8729521 | ||||
| timeCreated: 1690765399 | ||||
		Reference in New Issue
	
	Block a user