Skip to content

Commit e71e4f9

Browse files
committed
Optimized Code
-Updated Project Structure -Refactored naming convention -Updated code
1 parent e323a35 commit e71e4f9

10 files changed

Lines changed: 237 additions & 186 deletions
Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,38 @@ NavMeshSettings:
123123
debug:
124124
m_Flags: 0
125125
m_NavMeshData: {fileID: 0}
126+
--- !u!1 &379131701
127+
GameObject:
128+
m_ObjectHideFlags: 0
129+
m_CorrespondingSourceObject: {fileID: 0}
130+
m_PrefabInstance: {fileID: 0}
131+
m_PrefabAsset: {fileID: 0}
132+
serializedVersion: 6
133+
m_Component:
134+
- component: {fileID: 379131702}
135+
m_Layer: 0
136+
m_Name: Environment
137+
m_TagString: Untagged
138+
m_Icon: {fileID: 0}
139+
m_NavMeshLayer: 0
140+
m_StaticEditorFlags: 0
141+
m_IsActive: 1
142+
--- !u!4 &379131702
143+
Transform:
144+
m_ObjectHideFlags: 0
145+
m_CorrespondingSourceObject: {fileID: 0}
146+
m_PrefabInstance: {fileID: 0}
147+
m_PrefabAsset: {fileID: 0}
148+
m_GameObject: {fileID: 379131701}
149+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
150+
m_LocalPosition: {x: 24.182652, y: -6.827471, z: -9.924749}
151+
m_LocalScale: {x: 1, y: 1, z: 1}
152+
m_Children:
153+
- {fileID: 705507995}
154+
- {fileID: 2117160877}
155+
m_Father: {fileID: 0}
156+
m_RootOrder: 1
157+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
126158
--- !u!1 &705507993
127159
GameObject:
128160
m_ObjectHideFlags: 0
@@ -209,11 +241,11 @@ Transform:
209241
m_PrefabInstance: {fileID: 0}
210242
m_PrefabAsset: {fileID: 0}
211243
m_GameObject: {fileID: 705507993}
212-
m_LocalRotation: {x: 0.18121976, y: -0.22497801, z: 0.8798961, w: -0.37726262}
213-
m_LocalPosition: {x: 0, y: 3, z: 0}
244+
m_LocalRotation: {x: 0.18121977, y: -0.22497803, z: 0.87989616, w: -0.37726265}
245+
m_LocalPosition: {x: -24.182652, y: 9.827471, z: 9.924749}
214246
m_LocalScale: {x: 1, y: 1, z: 1}
215247
m_Children: []
216-
m_Father: {fileID: 0}
248+
m_Father: {fileID: 379131702}
217249
m_RootOrder: 0
218250
m_LocalEulerAnglesHint: {x: 15.021, y: -329.606, z: -129.483}
219251
--- !u!1 &963194225
@@ -305,7 +337,7 @@ Mesh:
305337
m_CorrespondingSourceObject: {fileID: 0}
306338
m_PrefabInstance: {fileID: 0}
307339
m_PrefabAsset: {fileID: 0}
308-
m_Name: pb_Mesh17438
340+
m_Name: pb_Mesh17238
309341
serializedVersion: 10
310342
m_SubMeshes:
311343
- serializedVersion: 2
@@ -793,11 +825,11 @@ Transform:
793825
m_PrefabInstance: {fileID: 0}
794826
m_PrefabAsset: {fileID: 0}
795827
m_GameObject: {fileID: 2117160872}
796-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
797-
m_LocalPosition: {x: 0, y: 0, z: 0}
828+
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
829+
m_LocalPosition: {x: -24.182652, y: 6.827471, z: 9.924749}
798830
m_LocalScale: {x: 1, y: 1, z: 1}
799831
m_Children: []
800-
m_Father: {fileID: 0}
832+
m_Father: {fileID: 379131702}
801833
m_RootOrder: 1
802834
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
803835
--- !u!4 &117881535607494348
@@ -1670,14 +1702,14 @@ Transform:
16701702
m_PrefabAsset: {fileID: 0}
16711703
m_GameObject: {fileID: 4416926081799734643}
16721704
m_LocalRotation: {x: 0, y: 0.7071068, z: 0, w: 0.7071068}
1673-
m_LocalPosition: {x: 9.46, y: 0.25, z: -9.42}
1705+
m_LocalPosition: {x: 9, y: 0.25, z: -9}
16741706
m_LocalScale: {x: 1, y: 1, z: 1}
16751707
m_Children:
16761708
- {fileID: 963194228}
16771709
- {fileID: 8002264680499780379}
16781710
- {fileID: 272993188726808912}
16791711
m_Father: {fileID: 0}
1680-
m_RootOrder: 2
1712+
m_RootOrder: 0
16811713
m_LocalEulerAnglesHint: {x: 0, y: 90, z: 0}
16821714
--- !u!4 &3964097221779534387
16831715
Transform:
@@ -1928,10 +1960,10 @@ MonoBehaviour:
19281960
m_EditorClassIdentifier:
19291961
AnimBlendSpeed: 8.9
19301962
MouseSensitivity: 21.9
1931-
upperLimit: -40
1932-
bottomlimit: 70
1963+
UpperLimit: -40
1964+
Bottomlimit: 70
19331965
CamHolder: {fileID: 8338988566224911407}
1934-
FPV: {fileID: 963194228}
1966+
Camera: {fileID: 963194228}
19351967
--- !u!114 &4416926081799734648
19361968
MonoBehaviour:
19371969
m_ObjectHideFlags: 0

Assets/_Scripts/InputManager.cs

Lines changed: 0 additions & 81 deletions
This file was deleted.

Assets/_Scripts/Manager.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using UnityEngine;
2+
using UnityEngine.InputSystem;
3+
using UnityEngine.InputSystem.Interactions;
4+
5+
namespace UnityTutorial.Manager
6+
{
7+
public class InputManager : MonoBehaviour
8+
{
9+
[SerializeField] private PlayerInput PlayerInput;
10+
11+
public Vector2 Move {get; private set;}
12+
public Vector2 Look {get; private set;}
13+
public bool Run {get; private set;}
14+
15+
16+
17+
private InputActionMap _currentMap;
18+
private InputAction _moveAction;
19+
private InputAction _lookAction;
20+
private InputAction _runAction;
21+
22+
23+
24+
private void Awake()
25+
{
26+
HideCursor();
27+
AssignReferences();
28+
SubscribeKeys();
29+
}
30+
31+
private static void HideCursor()
32+
{
33+
Cursor.visible = false;
34+
Cursor.lockState = CursorLockMode.Locked;
35+
}
36+
37+
private void SubscribeKeys()
38+
{
39+
_moveAction.performed += onMove;
40+
_lookAction.performed += onLook;
41+
_runAction.performed += onRun;
42+
43+
44+
_moveAction.canceled += onMove;
45+
_lookAction.canceled += onLook;
46+
_runAction.canceled += onRun;
47+
}
48+
49+
private void AssignReferences()
50+
{
51+
_currentMap = PlayerInput.currentActionMap;
52+
_moveAction = _currentMap.FindAction("Move");
53+
_lookAction = _currentMap.FindAction("Look");
54+
_runAction = _currentMap.FindAction("Run");
55+
}
56+
57+
private void onMove(InputAction.CallbackContext context)
58+
{
59+
Move = context.ReadValue<Vector2>();
60+
}
61+
62+
private void onLook(InputAction.CallbackContext context)
63+
{
64+
Look = context.ReadValue<Vector2>();
65+
}
66+
67+
private void onRun(InputAction.CallbackContext context)
68+
{
69+
Run = context.ReadValueAsButton();
70+
}
71+
72+
73+
74+
75+
private void OnEnable() {
76+
_currentMap.Enable();
77+
}
78+
79+
private void onDisable()
80+
{
81+
_currentMap.Disable();
82+
}
83+
84+
}
85+
}
File renamed without changes.

Assets/_Scripts/Player.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityTutorial.Manager;
6+
7+
namespace UnityTutorial.PlayerControl
8+
{
9+
public class PlayerController : MonoBehaviour
10+
{
11+
[SerializeField] private float AnimBlendSpeed = 0.5f;
12+
[SerializeField] private float MouseSensitivity = 10f;
13+
[SerializeField] private float UpperLimit = -40f;
14+
[SerializeField] private float Bottomlimit = 70f;
15+
[SerializeField] private Transform CamHolder;
16+
[SerializeField] private Transform Camera;
17+
18+
//Essential Constants
19+
private const float _runSpeed = 6f;
20+
private const float _walkSpeed = 2f;
21+
22+
//Variables
23+
private Vector2 _currrentVelocity;
24+
private float _xRotation;
25+
private float _yRotation;
26+
private bool _hasAnimator;
27+
private Animator _animator;
28+
private int _xVelHash;
29+
private int _yVelHash;
30+
31+
//Components
32+
private Rigidbody _playerRigidbody;
33+
private InputManager _inputManager;
34+
35+
36+
37+
private void Start() {
38+
_hasAnimator = TryGetComponent<Animator>(out _animator);
39+
_playerRigidbody = GetComponent<Rigidbody>();
40+
_inputManager = GetComponent<InputManager>();
41+
42+
43+
_xVelHash = Animator.StringToHash("X_Velocity");
44+
_yVelHash = Animator.StringToHash("Y_Velocity");
45+
}
46+
47+
48+
private void FixedUpdate()
49+
{
50+
Move();
51+
}
52+
53+
private void LateUpdate()
54+
{
55+
CamMovements();
56+
}
57+
58+
private void Move()
59+
{
60+
if(!_hasAnimator) return;
61+
62+
float targetSpeed = _inputManager.Run ? _runSpeed : _walkSpeed;
63+
if(_inputManager.Move == Vector2.zero) targetSpeed = 0.1f;
64+
65+
_currrentVelocity.x = Mathf.Lerp(_currrentVelocity.x , _inputManager.Move.x * targetSpeed, AnimBlendSpeed * Time.fixedDeltaTime);
66+
_currrentVelocity.y = Mathf.Lerp(_currrentVelocity.y , _inputManager.Move.y * targetSpeed, AnimBlendSpeed * Time.fixedDeltaTime);
67+
68+
var xVelDifference = _currrentVelocity.x - _playerRigidbody.velocity.x;
69+
var zVelDifference = _currrentVelocity.y - _playerRigidbody.velocity.z;
70+
_playerRigidbody.AddForce(transform.TransformVector(new Vector3(xVelDifference, 0 , zVelDifference)), ForceMode.VelocityChange);
71+
72+
_animator.SetFloat(_xVelHash, _currrentVelocity.x);
73+
_animator.SetFloat(_yVelHash, _currrentVelocity.y);
74+
}
75+
76+
private void CamMovements()
77+
{
78+
if(!_hasAnimator) return;
79+
80+
var Mouse_X = _inputManager.Look.x;
81+
var Mouse_Y = _inputManager.Look.y;
82+
Camera.position = CamHolder.position;
83+
84+
_xRotation -= Mouse_Y * MouseSensitivity * Time.deltaTime;
85+
_xRotation = Mathf.Clamp(_xRotation, UpperLimit , Bottomlimit);
86+
87+
Camera.localRotation = Quaternion.Euler(_xRotation, 0 , 0 );
88+
transform.Rotate(Vector3.up, Mouse_X * MouseSensitivity * Time.deltaTime);
89+
}
90+
91+
}
92+
}
File renamed without changes.

0 commit comments

Comments
 (0)