Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor controller logic from player controller to universal character controller #290

Merged
merged 46 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
3c7772f
REFACTOR: move movement to CharacterController
May 22, 2020
a15b7e5
REFACTOR: rotation so input method is not relevant
May 22, 2020
53c6fe9
REFACTOR: move rotation to character controller
May 22, 2020
19077d4
REFACTOR: extract method for playing sounds
May 22, 2020
8950a7c
REFACTOR: move character sounds to character controller
May 22, 2020
37d0470
REFACTOR: move check moveable terrain
May 22, 2020
541f156
FIX: automatic added loop dependency
May 22, 2020
6c56d97
REFACTOR: move jump
May 22, 2020
84d0cf9
FIX: typo
May 22, 2020
a15e16f
REFACTOR: use proper out variable
May 22, 2020
69d4abf
REFACTOR: simplify ifs
May 22, 2020
c180f52
REFACTOR: simplify another if
May 22, 2020
ba6f5c5
FIX: use correct verb for method name
May 22, 2020
94dfb97
REFACTOR: move sensitivity to outer scope
May 22, 2020
3f672f2
REFACTOR: move camera movement back to player controller
May 22, 2020
20691b3
REFACTOR: move is running flag
May 22, 2020
dfd4d4c
REFACTOR: move properties to outer scope
May 22, 2020
a6610d3
REFACTOR: remove redundant code
May 22, 2020
8926847
REFACTOR: move is airborne flag
May 22, 2020
a8e6611
REFACTOR: move rigid body to character controller
May 22, 2020
587d140
ADD: rigid body requirement
May 22, 2020
d8caac8
FIX: typo
May 22, 2020
b110ef4
REFACTOR: move is sneaking flag
May 22, 2020
6ceebbc
REMOVE: unused flag
May 22, 2020
7ecc106
REMOVE: more unused fields
May 22, 2020
9fa7fae
REFACTOR: move ground detector
May 22, 2020
eaffd55
REFACTOR: move jump power to outer scope
May 22, 2020
fc9bb41
REMOVE: unused parameter
May 22, 2020
20fc2ea
REMOVE: unused code
May 22, 2020
decf698
REFACTOR: move body field
May 22, 2020
911208e
REMOVE: unused parameter
May 22, 2020
1052308
UPDATE: use ground detector for feet position
May 22, 2020
122b89b
UPDATE: fields can be private
May 22, 2020
210e335
FIX: get ground detector
May 22, 2020
93252c9
REMOVE: debug log
May 22, 2020
d75d29f
FIX: get rigid body
May 22, 2020
9fe4cb7
FIX: body get dynamically
May 22, 2020
af87ab3
FIX: rotation
May 22, 2020
536d9c4
UPDATE: prefab to changes
May 22, 2020
b6331d0
REFACTOR: simplify if
May 22, 2020
0e5b3c3
UPDATE: method can be private
May 22, 2020
a60d358
REMOVE: white space
May 22, 2020
bdcecb5
REFACTOR: shorten code for diagonal walking
May 22, 2020
0e55204
FIX: minor readability issues
iTitus May 28, 2020
b26ad43
REFACtor: another minor
iTitus May 28, 2020
bb56c2e
FIX: side walk
May 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 39 additions & 58 deletions Assets/Prefabs/PlayerBelongings.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -2629,25 +2629,17 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 23851ed43df78d3489915c8a195d9036, type: 3}
m_Name:
m_EditorClassIdentifier:
body: {fileID: 2356435387424043641}
isAirborne: 0
isRunning: 0
isSneaking: 0
dialogueInterface: {fileID: 7994511833790087655}
interactor: {fileID: 2356435387424043642}
inventory: {fileID: 5251964727823270906}
mouseSensitivity: 250
pauseMenu: {fileID: 3412898498111374142}
playerCameraTransform: {fileID: 5496631978485877498}
rigidbody: {fileID: 2356435387424043640}
groundDetector: {fileID: 2356435386095074309}
interactor: {fileID: 2356435387424043642}
playerProperties: {fileID: 2356435387424043644}
questlog: {fileID: 0}
questDisplay: {fileID: 7585652203083821388}
questjournalSearchbar: {fileID: 7519064917230829377}
pauseMenu: {fileID: 3412898498111374142}
mouseSensitivity: 250
isRunning: 0
isSneaking: 0
sneakSlow: 0.7
isAirborne: 0
isSprinting: 0
sprintBoost: 1.3
dialogueInterface: {fileID: 7994511833790087655}
--- !u!65 &2356435387424043643
BoxCollider:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2712,6 +2704,7 @@ MonoBehaviour:
speed: {fileID: 11400000, guid: da106d5806969c14693d9c523cd6f87d, type: 2}
sneakMultiplier: 0.7
runMultiplier: 2
sidewaysMultiplier: 0.7
jumpPower: 450
weight: {fileID: 11400000, guid: 614a093e4ff687f47b74804bf6ef4bea, type: 2}
maxWeight: {fileID: 11400000, guid: 1b6144f0b378a564184f78a60f5a177e, type: 2}
Expand Down Expand Up @@ -8973,34 +8966,16 @@ PrefabInstance:
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 24317b369c54e2143af902331f595adf, type: 3}
--- !u!1 &4666713041352449235 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1338428400787657039, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
--- !u!1 &3047925629568477781 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 8654822773299619785, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
--- !u!224 &7567846488276705717 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 4275630235739450409, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1099457508181913526 stripped
--- !u!114 &7585652203083821388 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 6706560802818712106, guid: 24317b369c54e2143af902331f595adf,
m_CorrespondingSourceObject: {fileID: 4257543492969479376, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4666713041352449235}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e64b2bbe094026a4299165af1a474ab1, type: 3}
m_Script: {fileID: 11500000, guid: 422b3d1f0377a0d47acb782b4a7682ad, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &4135858231327417772 stripped
Expand All @@ -9015,18 +8990,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: f4e3ec7d8aceeb94c84f0b3d14916afd, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &7585652203083821388 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 4257543492969479376, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4666713041352449235}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 422b3d1f0377a0d47acb782b4a7682ad, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &8742905801970135246 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 3100017794379513170, guid: 24317b369c54e2143af902331f595adf,
Expand All @@ -9039,39 +9002,57 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &2356435386368203710 stripped
--- !u!114 &7383090374177351392 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 8278687299762640418, guid: 24317b369c54e2143af902331f595adf,
m_CorrespondingSourceObject: {fileID: 3757817180409998204, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Script: {fileID: 11500000, guid: 365c0483be2f4425be4ad86611a43d47, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &7383090374177351392 stripped
--- !u!1 &4666713041352449235 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1338428400787657039, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
--- !u!1 &3047925629568477781 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 8654822773299619785, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
--- !u!224 &7567846488276705717 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 4275630235739450409, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1099457508181913526 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 3757817180409998204, guid: 24317b369c54e2143af902331f595adf,
m_CorrespondingSourceObject: {fileID: 6706560802818712106, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_GameObject: {fileID: 4666713041352449235}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 365c0483be2f4425be4ad86611a43d47, type: 3}
m_Script: {fileID: 11500000, guid: e64b2bbe094026a4299165af1a474ab1, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &7519064917230829377 stripped
--- !u!114 &2356435386368203710 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 4181986942095061725, guid: 24317b369c54e2143af902331f595adf,
m_CorrespondingSourceObject: {fileID: 8278687299762640418, guid: 24317b369c54e2143af902331f595adf,
type: 3}
m_PrefabInstance: {fileID: 5931367441388064156}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2da0c512f12947e489f739169773d7ca, type: 3}
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
45 changes: 45 additions & 0 deletions Assets/Scenes/TestScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -8893,6 +8893,31 @@ PrefabInstance:
propertyPath: m_Name
value: PlayerBelongings
objectReference: {fileID: 0}
- target: {fileID: 2356435387424043641, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2356435387424043641, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2356435387424043641, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2356435387424043641, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2356435387424043641, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4135858231327417772, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: questInteractables
Expand All @@ -8913,6 +8938,26 @@ PrefabInstance:
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6115537310667178606, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_Name
value: Player
objectReference: {fileID: 0}
- target: {fileID: 7099248684761739389, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7099248684761739389, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 7099248684761739389, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8218561273732356039, guid: d5177fd664a14c0418bd57f76722b517,
type: 3}
propertyPath: m_AnchorMin.y
Expand Down
154 changes: 154 additions & 0 deletions Assets/Scripts/Character/CharacterController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using Character.Player;
using Sounds.Manager;
using UnityEngine;

namespace Character
{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CharacterSounds))]
public class CharacterController : MonoBehaviour

{
private GroundDetector _groundDetector;
private Rigidbody _rigidbody;

[Header("State Attributes")] public JumpStatus isAirborne;
public bool isRunning;
public bool isSneaking;

public CharacterSounds Sounds { get; set; }

protected virtual void Start()
{
_rigidbody = GetComponent<Rigidbody>();
_groundDetector = GetComponentInChildren<GroundDetector>();
Sounds = GetComponent<CharacterSounds>();
}

protected virtual void Update()
{
// check if the player in the Air or not
isAirborne = _groundDetector.currentCollisions.Count == 0 ? JumpStatus.InAir : JumpStatus.OnGround;
}

public void Movement(float horizontal, float vertical, CharacterProperties properties)
{
float speed = properties.speed.Value;
float runMultiplier = properties.runMultiplier;
float sneakMultiplier = properties.sneakMultiplier;
float sidewaysMultiplier = properties.sidewaysMultiplier;

// TODO: fully convert to StatAttribute
// get the actual speed with all modifications
if (isRunning)
speed *= runMultiplier;
if (isSneaking)
speed *= sneakMultiplier;

// makes sure that sideways/backwards walking is slower than forward walking
if (Mathf.Abs(horizontal) > 0.01 || vertical < -0.01)
speed *= sidewaysMultiplier;

Vector3 inputVelocity = (transform.forward * vertical) + (transform.right * horizontal);
Vector3 inputPlaneVelocity = new Vector3(inputVelocity.x, 0, inputVelocity.z).normalized;

if (CheckWalkableTerrain(_groundDetector.transform.position, inputPlaneVelocity, 5f))
{
// makes sure, that the total velocity is not higher while walking cross-ways
if (inputVelocity.magnitude > 1.01)
{
inputVelocity.x = inputPlaneVelocity.x;
inputVelocity.z = inputPlaneVelocity.z;
}

inputVelocity *= speed;

// manages movement depending on being airborne or not
if (isAirborne == JumpStatus.OnGround)
{
inputVelocity.y = _rigidbody.velocity.y;
_rigidbody.velocity = inputVelocity;
}
else
{
inputVelocity.y = 0;

_rigidbody.AddForce(inputVelocity, ForceMode.Impulse);

// make sure, that the player is not able to be faster then the momentarily speed level is allowing him to be
inputVelocity = _rigidbody.velocity;
inputVelocity.y = 0;
inputVelocity = inputVelocity.normalized * Mathf.Clamp(inputVelocity.magnitude, 0, speed);
inputVelocity.y = _rigidbody.velocity.y;

_rigidbody.velocity = inputVelocity;
}
}
else
{
// stops the player at an instant if the terrain is not movable
_rigidbody.velocity = Vector3.zero;
}

PlaySoundForMovement(inputVelocity);
}

private void PlaySoundForMovement(Vector3 velocity)
{
if (isRunning && velocity.magnitude > 0.1f && isAirborne == JumpStatus.OnGround)
{
Sounds.Running(_groundDetector.GroundType);
}
else if (isSneaking && velocity.magnitude > 0.1f && isAirborne == JumpStatus.OnGround)
{
Sounds.Sneaking(_groundDetector.GroundType);
}
//TODO: replace with isWalking flag
else if (isAirborne == 0 && velocity.magnitude > 0.1f)
{
Sounds.Walking(_groundDetector.GroundType);
}
else
{
Sounds.StopMovement();
}
}

public void Rotation(float rotationX, float sensitivity = 1f)
{
// get mouse Inputs
rotationX = Mathf.Clamp(rotationX, -10, 10);

Vector3 bodyRotation = new Vector3(0, rotationX, 0);
transform.Rotate(bodyRotation * (sensitivity * Time.deltaTime), Space.Self);
}

public bool CheckWalkableTerrain(Vector3 position, Vector3 desiredDirection, float distance)
{
Ray slopeRay = new Ray(position, desiredDirection);

if (!Physics.Raycast(slopeRay, out RaycastHit hit, distance))
return true;
if (hit.collider.gameObject.tag is "Interactable")
return true;

// get the angle between the up vector and the hit game object
float slopeAngle = Vector3.Angle(Vector3.up, hit.normal);
if (slopeAngle <= 45f)
return true;
return hit.distance >= 0.26f;
}

public void Jump(float jumpPower)
{
if (isAirborne != JumpStatus.OnGround)
return;

Vector3 vel = _rigidbody.velocity;
vel.y = 0;
_rigidbody.velocity = vel;

_rigidbody.AddForce(jumpPower * Vector3.up, ForceMode.Impulse);
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Character/CharacterController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Scripts/Character/CharacterProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public abstract class CharacterProperties : AttributeHolder
[Header("Speed values")] public StatAttribute speed;
public float sneakMultiplier = 0.7f;
public float runMultiplier = 2f;
public float sidewaysMultiplier = 0.7f;

public float jumpPower = 450f;

Expand Down
Loading