From edea89cc59bb863c1854033025c7375d18b07324 Mon Sep 17 00:00:00 2001 From: Jason Durand 01 Date: Fri, 1 Apr 2022 23:35:34 -0400 Subject: [PATCH] Added quick joystick to move + scriptableobject --- Assets/Prefabs/Vampire.prefab | 40 ++++++++++++++++++++++ Assets/Scripts/Player Stats.asset | 16 +++++++++ Assets/Scripts/Player Stats.asset.meta | 8 +++++ Assets/Scripts/PlayerMovement.cs | 46 +++++++++++++++++--------- Assets/Scripts/PlayerStats.cs | 7 ++++ Assets/Scripts/PlayerStats.cs.meta | 3 ++ 6 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 Assets/Scripts/Player Stats.asset create mode 100644 Assets/Scripts/Player Stats.asset.meta create mode 100644 Assets/Scripts/PlayerStats.cs create mode 100644 Assets/Scripts/PlayerStats.cs.meta diff --git a/Assets/Prefabs/Vampire.prefab b/Assets/Prefabs/Vampire.prefab index 12ca989..ecc8965 100644 --- a/Assets/Prefabs/Vampire.prefab +++ b/Assets/Prefabs/Vampire.prefab @@ -12,6 +12,8 @@ GameObject: - component: {fileID: 1214567908930553595} - component: {fileID: 1214567908930553592} - component: {fileID: 1214567908930553477} + - component: {fileID: 945832017} + - component: {fileID: 945832018} m_Layer: 0 m_Name: Vampire m_TagString: Untagged @@ -96,6 +98,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 639be549366b46748bd86eb6cea796a3, type: 3} m_Name: m_EditorClassIdentifier: + playerStats: {fileID: 11400000, guid: 12a626b5a296d934ba078d222ad6ba98, type: 2} --- !u!114 &1214567908930553477 MonoBehaviour: m_ObjectHideFlags: 0 @@ -178,3 +181,40 @@ MonoBehaviour: m_DefaultActionMap: Player m_SplitScreenIndex: -1 m_Camera: {fileID: 0} +--- !u!50 &945832017 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1214567908930553593} + m_BodyType: 1 + m_Simulated: 1 + m_UseFullKinematicContacts: 1 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 1 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 +--- !u!58 &945832018 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1214567908930553593} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + serializedVersion: 2 + m_Radius: 0.5 diff --git a/Assets/Scripts/Player Stats.asset b/Assets/Scripts/Player Stats.asset new file mode 100644 index 0000000..eda295e --- /dev/null +++ b/Assets/Scripts/Player Stats.asset @@ -0,0 +1,16 @@ +%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: 5a9c2f4541a74e86afc424d568e0f629, type: 3} + m_Name: Player Stats + m_EditorClassIdentifier: + movementSpeed: 3 + suckSpeed: 1 diff --git a/Assets/Scripts/Player Stats.asset.meta b/Assets/Scripts/Player Stats.asset.meta new file mode 100644 index 0000000..b4f3d1a --- /dev/null +++ b/Assets/Scripts/Player Stats.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 12a626b5a296d934ba078d222ad6ba98 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index e110952..3607af9 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -1,18 +1,34 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; +using UnityEngine.InputSystem; -public class PlayerMovement : MonoBehaviour -{ - // Start is called before the first frame update - void Start() - { - - } +[RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))] +public class PlayerMovement : MonoBehaviour { + [SerializeField] PlayerStats playerStats; + + PlayerInput playerInput; + Vector2 moveDirection; - // Update is called once per frame - void Update() - { - - } -} + void Start() { + playerInput = GetComponent(); + + playerInput.actions["Move"].started += OnMove; + playerInput.actions["Move"].performed += OnMove; + playerInput.actions["Move"].canceled += OnMove; + } + + void OnDestroy() { + playerInput.actions["Move"].started -= OnMove; + playerInput.actions["Move"].performed -= OnMove; + playerInput.actions["Move"].canceled -= OnMove; + } + + void Update() { + transform.position += (Vector3)moveDirection * Time.deltaTime * playerStats.movementSpeed; + } + + void OnMove(InputAction.CallbackContext ctx) { + moveDirection = ctx.ReadValue(); + if (moveDirection.sqrMagnitude > 1.0f) + moveDirection.Normalize(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerStats.cs b/Assets/Scripts/PlayerStats.cs new file mode 100644 index 0000000..69f8edc --- /dev/null +++ b/Assets/Scripts/PlayerStats.cs @@ -0,0 +1,7 @@ +using UnityEngine; + +[CreateAssetMenu] +public class PlayerStats : ScriptableObject { + public float movementSpeed = 3f; + public float suckSpeed = 1f; +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerStats.cs.meta b/Assets/Scripts/PlayerStats.cs.meta new file mode 100644 index 0000000..3c3dd55 --- /dev/null +++ b/Assets/Scripts/PlayerStats.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5a9c2f4541a74e86afc424d568e0f629 +timeCreated: 1648869672 \ No newline at end of file