Skip to content

Commit

Permalink
Merge pull request #19 from ErithacusProject/patches/camera-and-movement
Browse files Browse the repository at this point in the history
Camera-Dependant Character Movement Completed
  • Loading branch information
MultidimensionalSock authored Oct 13, 2024
2 parents 5b795ea + a017b13 commit 4c1144a
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 84 deletions.
8 changes: 5 additions & 3 deletions Lares/Assets/Camera/Prefabs/PlayerCamera.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ MonoBehaviour:
_camera: {fileID: 3085492360813940509}
_cameraCenter: {x: 0, y: 0, z: 0}
_cameraOffset: {x: 0, y: 0}
_distanceFromCenter: 0
_distanceFromCenter: 3
_mouseSensitivity: 1
_controllerSensitivity: 1
--- !u!114 &6797900550905813070
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -78,7 +80,7 @@ MonoBehaviour:
m_ActionEvents: []
m_NeverAutoSwitchControlSchemes: 0
m_DefaultControlScheme:
m_DefaultActionMap: 52975b73-2aa1-4539-89d0-52d2579b712a
m_DefaultActionMap: BaseControls
m_SplitScreenIndex: -1
m_Camera: {fileID: 0}
--- !u!1 &7343679560158206760
Expand Down Expand Up @@ -109,7 +111,7 @@ Transform:
m_GameObject: {fileID: 7343679560158206760}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
Expand Down
67 changes: 45 additions & 22 deletions Lares/Assets/Camera/Scripts/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,92 @@ public class CameraController : MonoBehaviour
{
[Header("Objects")]
[SerializeField] private UnityEngine.Camera _camera;
[SerializeField] private PlayerInput _playerInput;

[Header("Camera Settings")]
[SerializeField] private Vector3 _cameraCenter;
[SerializeField] private Vector2 _cameraOffset;
[SerializeField] private float _distanceFromCenter;

[Header("Input Settings")]
[SerializeField, Range(0.01f, 2f)] private float _mouseSensitivity = 1f;
[SerializeField, Range(0.01f, 2f)] private float _controllerSensitivity = 1f;


private PlayerControls _playerControls;

private Vector2 _inputVector;

private void Awake()
{
if (!Application.isPlaying) return;

_playerControls = new PlayerControls();

if (!_playerInput)
Debug.LogError("Player Input not found!");
}

private void Start()
{
transform.localPosition = _cameraCenter;
transform.localRotation = Quaternion.identity;
_camera.transform.localPosition = new Vector3(_cameraOffset.x, _cameraOffset.y, -_distanceFromCenter);
}

private void OnEnable()
{
if (!Application.isPlaying) return;

_playerControls.Enable();
_playerControls.BaseControls.MoveCamera.performed += OnMoveCameraPerformed;
_playerControls.BaseControls.MoveCamera.canceled += OnMoveCameraCanceled;
}

private void OnDisable()
{
if (!Application.isPlaying) return;

_playerControls.Disable();
_playerControls.BaseControls.MoveCamera.performed -= OnMoveCameraPerformed;
_playerControls.BaseControls.MoveCamera.canceled -= OnMoveCameraCanceled;

_playerControls.Disable();
}

private void LateUpdate()
{
if (!Application.isPlaying) return;

transform.localPosition = _cameraCenter;
_camera.transform.localPosition = new Vector3(_cameraOffset.x, _cameraOffset.y, -_distanceFromCenter);
if (_camera.transform.localPosition != (Vector3)_cameraOffset)
{
_camera.transform.localPosition = new Vector3(_cameraOffset.x, _cameraOffset.y, -_distanceFromCenter);
}

_inputVector.y = Mathf.Clamp(_inputVector.y, -90f, 90);
switch (_inputVector.x)
if (_inputVector == Vector2.zero)
return;

switch (_playerInput.currentControlScheme)
{
case >= 360f:
_inputVector.x -= 360f;
case "Controller":
_inputVector *= _controllerSensitivity;
break;
case <= -360f:
_inputVector.x += 360f;
case "Keyboard and Mouse":
_inputVector *= _mouseSensitivity;
break;
}

Vector3 currentRotation = transform.localEulerAngles;
currentRotation.x += _inputVector.y;
currentRotation.y -= _inputVector.x;

transform.localRotation = Quaternion.Euler(_inputVector.y, -_inputVector.x, 0);
}
currentRotation.x = currentRotation.x switch
{
>= 180f and < 270f => 270f,
> 90f and < 180f => 90f,
_ => currentRotation.x
};

transform.localRotation = Quaternion.Euler(currentRotation);
}

private void OnMoveCameraPerformed(InputAction.CallbackContext context)
{
_inputVector += context.ReadValue<Vector2>();
_inputVector = context.ReadValue<Vector2>();
}

private void OnMoveCameraCanceled(InputAction.CallbackContext context)
{
_inputVector = Vector2.zero;
}
}
}
2 changes: 1 addition & 1 deletion Lares/Assets/Player/PlayerControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public @PlayerControls()
{
""name"": ""2D Vector"",
""id"": ""4ae4cee4-816b-426b-9e6e-e4f0aed0122e"",
""path"": ""2DVector"",
""path"": ""2DVector(mode=2)"",
""interactions"": """",
""processors"": """",
""groups"": """",
Expand Down
2 changes: 1 addition & 1 deletion Lares/Assets/Player/PlayerControls.inputactions
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
{
"name": "2D Vector",
"id": "4ae4cee4-816b-426b-9e6e-e4f0aed0122e",
"path": "2DVector",
"path": "2DVector(mode=2)",
"interactions": "",
"processors": "",
"groups": "",
Expand Down
3 changes: 1 addition & 2 deletions Lares/Assets/Player/Scripts/PlayerAnimation.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

namespace Lares
namespace Lares.Player.Scripts
{
public class PlayerAnimation : MonoBehaviour
{
Expand Down
92 changes: 44 additions & 48 deletions Lares/Assets/Player/Scripts/PlayerMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
using UnityEngine;
using UnityEngine.InputSystem;

namespace Lares
namespace Lares.Player.Scripts
{
public class PlayerMovement : MonoBehaviour
{
PlayerInput _input;
Vector3 moveDirection;
Rigidbody _rigidbody;
Coroutine _moveCoroutine;
int _jumpNo = 0;
[SerializeField] float movementForce;
[SerializeField] float maxSpeed;
[SerializeField] float jumpForce;
[SerializeField] float rotationSpeed;


// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
private PlayerInput _input;
private Vector3 _moveDirection;
private Rigidbody _rigidbody;
private Coroutine _moveCoroutine;
private int _jumpNo;

[SerializeField] private float _movementForce;
[SerializeField] private float _maxSpeed;
[SerializeField] private float _jumpForce;
[SerializeField] private float _rotationSpeed;

[SerializeField] private Transform _cameraTransform;

private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
_input = GetComponent<PlayerInput>();
Expand All @@ -30,66 +31,61 @@ void Start()
_input.currentActionMap.FindAction("Evade").performed += Evade;
}

#region move player
void MoveStart(InputAction.CallbackContext context)
#region Player Movement

private void MoveStart(InputAction.CallbackContext context)
{
Vector2 movementInput = context.ReadValue<Vector2>();

moveDirection = new Vector3(movementInput.x, 0f, movementInput.y).normalized;

moveDirection = new Vector3(movementInput.x, 0f, movementInput.y);

if (_moveCoroutine == null) { StartCoroutine(Move()); }


_moveDirection = new Vector3(movementInput.x, 0f, movementInput.y);
_moveCoroutine ??= StartCoroutine(Move());
}

void MoveEnd(InputAction.CallbackContext context)
private void MoveEnd(InputAction.CallbackContext context)
{
moveDirection = Vector3.zero;
_moveDirection = Vector3.zero;
}

IEnumerator Move()
private IEnumerator Move()
{
GameObject cameraLook = transform.GetChild(0).gameObject;
GameObject model = transform.GetChild(1).gameObject;

while (moveDirection != Vector3.zero)
while (_moveDirection != Vector3.zero)
{
_rigidbody.MovePosition(transform.position + (moveDirection * movementForce) * Time.fixedDeltaTime);
Quaternion targetRot = cameraLook.transform.rotation * Quaternion.LookRotation(moveDirection, Vector3.up);
model.transform.rotation = Quaternion.Slerp(model.transform.rotation, targetRot, rotationSpeed);

model.transform.rotation = Quaternion.Slerp(model.transform.rotation,
Quaternion.Euler(0, _cameraTransform.rotation.eulerAngles.y, 0), _rotationSpeed);

_rigidbody.AddForce(model.transform.forward * (_moveDirection.z * _movementForce * Time.fixedDeltaTime),
ForceMode.VelocityChange);
_rigidbody.AddForce(model.transform.right * (_moveDirection.x * _movementForce * Time.fixedDeltaTime),
ForceMode.VelocityChange);

yield return new WaitForFixedUpdate();
}
//_rigidbody.maxAngularVelocity = _rigidbody.maxAngularVelocity / 1.5f;

_moveCoroutine = null;
}

#endregion

#region Jump

void Jump(InputAction.CallbackContext context)

private void Jump(InputAction.CallbackContext context)
{
if (_jumpNo < 2)
{
_rigidbody.AddForce(new Vector3(0f, jumpForce - _rigidbody.linearVelocity.y, 0f), ForceMode.Impulse);
_rigidbody.AddForce(new Vector3(0f, _jumpForce - _rigidbody.linearVelocity.y, 0f), ForceMode.Impulse);
_jumpNo++;
}
}
#endregion

#region Interact
void Interact(InputAction.CallbackContext context)

private void Interact(InputAction.CallbackContext context)
{

}
#endregion

#region Evade
void Evade(InputAction.CallbackContext context) { }
#endregion


private void Evade(InputAction.CallbackContext context)
{
}

private void OnCollisionEnter(Collision collision)
{
_jumpNo = 0;
Expand Down
Loading

0 comments on commit 4c1144a

Please sign in to comment.