basic camera controller

This commit is contained in:
cyndrdev 2021-01-27 12:06:17 +00:00
parent ab7cd608cb
commit 7fefaae405
14 changed files with 181 additions and 6 deletions

View File

@ -706,7 +706,7 @@ MonoBehaviour:
m_ActionId: 2bbc2e93-6016-4544-8399-0c4ee222850c
m_ActionName: CoreMap/Use[/Keyboard/e,/XInputControllerWindows/buttonWest]
m_NeverAutoSwitchControlSchemes: 0
m_DefaultControlScheme: PCMR
m_DefaultControlScheme:
m_DefaultActionMap: CoreMap
m_SplitScreenIndex: -1
m_Camera: {fileID: 1444606338}
@ -751,6 +751,7 @@ GameObject:
- component: {fileID: 1444606338}
- component: {fileID: 1444606337}
- component: {fileID: 1444606340}
- component: {fileID: 1444606341}
m_Layer: 0
m_Name: PlayerCamera
m_TagString: MainCamera
@ -855,6 +856,20 @@ MonoBehaviour:
m_RequiresDepthTexture: 0
m_RequiresColorTexture: 0
m_Version: 2
--- !u!114 &1444606341
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1444606336}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 805caadbe4df94a40bf7ddb70bea02f6, type: 3}
m_Name:
m_EditorClassIdentifier:
_settings: {fileID: 11400000, guid: 995f378ab762cd344b7a6d108f049191, type: 2}
_inputHandler: {fileID: 1292675913}
--- !u!1 &1468685575
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c3040315fc41e344b95af9bba0d16776
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,42 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField]
private CameraSettings _settings;
[SerializeField]
private PlayerInputHandler _inputHandler;
private Vector2 _cameraRotation;
void FixedUpdate()
{
float deltaTime = Time.fixedDeltaTime;
_cameraRotation += _inputHandler.InputState.Look * ( deltaTime * _settings.LookSpeed );
_cameraRotation.y = Mathf.Clamp( _cameraRotation.y, _settings.LookYMin, _settings.LookYMax );
_inputHandler.SetCameraRotation(_cameraRotation.x * -180.0f);
}
private void LateUpdate()
{
if ( _cameraRotation.x > 1.0f )
{
_cameraRotation.x -= 2.0f;
}
else if ( _cameraRotation.x < -1.0f )
{
_cameraRotation.x += 2.0f;
}
Quaternion rot = Quaternion.Euler( _cameraRotation.y * 180.0f, _cameraRotation.x * 180.0f, 0f );
Vector3 pos = rot * ( Vector3.back * _settings.LookDistance );
transform.localPosition = pos;
transform.rotation = rot;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 805caadbe4df94a40bf7ddb70bea02f6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "CameraSettings.asset", menuName = "KernelPanic/Camera/Settings")]
public class CameraSettings : ScriptableObject
{
[Header("Speed")]
public float LookSpeed;
[Header("Constraints")]
public float LookDistance;
public float LookYMin;
public float LookYMax;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b67d3eff4427cf14182daa0548676571
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public struct CameraRelativeInput
{
private Vector2 _base;
private float _cameraAngle;
public void SetValue( Vector2 value )
{
_base = value;
}
public void SetAngle( float angle )
{
_cameraAngle = angle;
}
public Vector2 Value
=> Quaternion.Euler( 0f, 0f, _cameraAngle ) * _base;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2001265c10e110042a86e193cff6f419
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -20,7 +20,7 @@ public class PlayerController : MonoBehaviour
private void FixedUpdate()
{
Vector2 moveVector2 = _inputHandler.InputState.Move;
Vector2 moveVector2 = _inputHandler.InputState.Move.Value;
Vector3 moveVector = new Vector3( moveVector2.x, 0f, moveVector2.y );
_rigidbody.velocity = moveVector * _movementSettings.BaseMovementSpeed;

View File

@ -13,8 +13,9 @@ public class PlayerInputHandler : MonoBehaviour
{
public BufferedInput Jump;
public BufferedInput Use;
public Vector2 Move;
public CameraRelativeInput Move;
public Vector2 Look;
public float MoveRotation;
}
public PlayerInputState InputState => _state;
@ -27,8 +28,8 @@ public class PlayerInputHandler : MonoBehaviour
_state.Jump = new BufferedInput( _inputSettings.JumpBufferTime );
_state.Use = new BufferedInput( _inputSettings.UseBufferTime );
_state.Move = new CameraRelativeInput();
_state.Look = Vector2.zero;
_state.Move = Vector2.zero;
}
private void FixedUpdate()
@ -39,11 +40,14 @@ public class PlayerInputHandler : MonoBehaviour
_state.Use.Update( deltaTime );
}
public void SetCameraRotation( float angle )
=> _state.Move.SetAngle( angle );
public void Look( InputAction.CallbackContext context )
=> _state.Look = context.ReadValue<Vector2>();
public void Move( InputAction.CallbackContext context )
=> _state.Move = context.ReadValue<Vector2>();
=> _state.Move.SetValue( context.ReadValue<Vector2>() );
public void Jump( InputAction.CallbackContext context )
=> _state.Jump.Set( context.ReadValue<float>() > 0.5f );

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4b0a99647895c3d45bf1200c49d99d31
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b67d3eff4427cf14182daa0548676571, type: 3}
m_Name: CameraSettings
m_EditorClassIdentifier:
LookSpeed: 1
LookDistance: 8
LookYMin: 0.025
LookYMax: 0.5

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 995f378ab762cd344b7a6d108f049191
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -110,7 +110,7 @@
"id": "f11a0fcc-85b6-41ab-adbb-e9630b5bfdaa",
"path": "<Mouse>/delta",
"interactions": "",
"processors": "",
"processors": "ScaleVector2(x=0.1,y=0.1)",
"groups": "PCMR",
"action": "Look",
"isComposite": false,