Skip to content

Commit

Permalink
Camera-Dependant Character Movement Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethirix committed Oct 13, 2024
1 parent 5931397 commit a017b13
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 61 deletions.
9 changes: 5 additions & 4 deletions Lares/Assets/Camera/Scripts/CameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Lares.Camera.Scripts
//Determine if Camera is in correct position
//Raycast between Camera and center, ignore the player somehow

[RequireComponent(typeof(PlayerInput))]
public class CameraController : MonoBehaviour
{
[Header("Objects")]
[SerializeField] private UnityEngine.Camera _camera;
[SerializeField] private PlayerInput _playerInput;

[Header("Camera Settings")]
[SerializeField] private Vector2 _cameraOffset;
Expand All @@ -20,22 +20,23 @@ public class CameraController : MonoBehaviour
[SerializeField, Range(0.01f, 2f)] private float _mouseSensitivity = 1f;
[SerializeField, Range(0.01f, 2f)] private float _controllerSensitivity = 1f;

private PlayerInput _playerInput;

private PlayerControls _playerControls;

private Vector2 _inputVector;

private void Awake()
{
_playerControls = new PlayerControls();

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

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

_playerInput = GetComponent<PlayerInput>();
}

private void OnEnable()
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
125 changes: 118 additions & 7 deletions Lares/Assets/Scenes/Test.unity
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!4 &145460218 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
m_PrefabInstance: {fileID: 1154946001}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &235956305
PrefabInstance:
m_ObjectHideFlags: 0
Expand All @@ -129,17 +135,17 @@ PrefabInstance:
- target: {fileID: -8679921383154817045, guid: d6bffbf0d3b43f247bc9e6505c8f2e53,
type: 3}
propertyPath: m_LocalPosition.x
value: -1.1350164
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: d6bffbf0d3b43f247bc9e6505c8f2e53,
type: 3}
propertyPath: m_LocalPosition.y
value: -0.19335985
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: d6bffbf0d3b43f247bc9e6505c8f2e53,
type: 3}
propertyPath: m_LocalPosition.z
value: -0.75197625
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: d6bffbf0d3b43f247bc9e6505c8f2e53,
type: 3}
Expand Down Expand Up @@ -395,10 +401,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 8457699371b065f42a5afc085ce43534, type: 3}
m_Name:
m_EditorClassIdentifier:
movementForce: 5
maxSpeed: 4
jumpForce: 5
rotationSpeed: 0.1
_movementForce: 10
_maxSpeed: 4
_jumpForce: 5
_rotationSpeed: 0.1
_cameraTransform: {fileID: 145460218}
--- !u!54 &490723952
Rigidbody:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -472,6 +479,7 @@ Transform:
m_Children:
- {fileID: 263515828}
- {fileID: 1507139258}
- {fileID: 145460218}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!136 &490723955
Expand Down Expand Up @@ -684,6 +692,109 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1001 &1154946001
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 490723954}
m_Modifications:
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalPosition.y
value: 1.355
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2379264742062872151, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2511604713696256102, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_Name
value: PlayerCamera
objectReference: {fileID: 0}
- target: {fileID: 3152537829569927056, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3152537829569927056, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_LocalPosition.z
value: -1
objectReference: {fileID: 0}
- target: {fileID: 6797900550905813070, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_Enabled
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6797900550905813070, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: m_DefaultActionMap
value: BaseControls
objectReference: {fileID: 0}
- target: {fileID: 8414152557787723358, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: _playerInput
value:
objectReference: {fileID: 490723953}
- target: {fileID: 8414152557787723358, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: _mouseSensitivity
value: 0.2
objectReference: {fileID: 0}
- target: {fileID: 8414152557787723358, guid: 198d9f6896f5af54989a7e5d663af39e,
type: 3}
propertyPath: _distanceFromCenter
value: 3
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 198d9f6896f5af54989a7e5d663af39e, type: 3}
--- !u!1 &1238714000
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit a017b13

Please sign in to comment.