From 1cc381c1a924ad0b8ad0256dd4ed5f170a1dd2a3 Mon Sep 17 00:00:00 2001 From: Phobez <36761666+Phobez@users.noreply.github.com> Date: Fri, 21 Apr 2023 15:36:33 +0900 Subject: [PATCH] Implement unit movement --- Assets/Prefabs/Unit.prefab | 36 ++++++ Assets/Scenes/SampleScene.unity | 116 +++++++++--------- Assets/Scripts/Unit.cs | 9 ++ .../{UnitSelector.cs => UnitController.cs} | 40 +++++- ...elector.cs.meta => UnitController.cs.meta} | 0 Assets/Scripts/UnitMovement.cs | 39 ++++++ Assets/Scripts/UnitMovement.cs.meta | 11 ++ UserSettings/Layouts/default-2021.dwlt | 64 +++++----- 8 files changed, 224 insertions(+), 91 deletions(-) rename Assets/Scripts/{UnitSelector.cs => UnitController.cs} (69%) rename Assets/Scripts/{UnitSelector.cs.meta => UnitController.cs.meta} (100%) create mode 100644 Assets/Scripts/UnitMovement.cs create mode 100644 Assets/Scripts/UnitMovement.cs.meta diff --git a/Assets/Prefabs/Unit.prefab b/Assets/Prefabs/Unit.prefab index 28bd42e..0526d3d 100644 --- a/Assets/Prefabs/Unit.prefab +++ b/Assets/Prefabs/Unit.prefab @@ -12,6 +12,8 @@ GameObject: - component: {fileID: 4230525904207381883} - component: {fileID: 4230525904207381880} - component: {fileID: 4230525904207381881} + - component: {fileID: -6529376481025236616} + - component: {fileID: -8548596752862253275} m_Layer: 0 m_Name: Unit m_TagString: Untagged @@ -116,6 +118,40 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: selectedIndicator: {fileID: 5503896601731364458} +--- !u!50 &-6529376481025236616 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4230525904207381876} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 4 +--- !u!114 &-8548596752862253275 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4230525904207381876} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 87670e1830ba2774bb8e7efda1a9a330, type: 3} + m_Name: + m_EditorClassIdentifier: + speed: 5 --- !u!1 &5503896601731364458 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index e8b2b98..b5ee226 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -206,7 +206,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!60 &336780108 PolygonCollider2D: @@ -236,63 +236,6 @@ PolygonCollider2D: - - {x: 0, y: 0.5773587} - {x: -0.5, y: -0.28866667} - {x: 0.5, y: -0.28866667} ---- !u!1001 &389938922 -PrefabInstance: - m_ObjectHideFlags: 0 - serializedVersion: 2 - m_Modification: - m_TransformParent: {fileID: 0} - m_Modifications: - - target: {fileID: 4230525904207381876, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_Name - value: Unit (1) - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_RootOrder - value: 4 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalPosition.x - value: 2.076311 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalPosition.y - value: 1.1068352 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalPosition.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalRotation.w - value: 1 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalRotation.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalRotation.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalRotation.z - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalEulerAnglesHint.x - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalEulerAnglesHint.y - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} - propertyPath: m_LocalEulerAnglesHint.z - value: 0 - objectReference: {fileID: 0} - m_RemovedComponents: [] - m_SourcePrefab: {fileID: 100100000, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} --- !u!1 &519420028 GameObject: m_ObjectHideFlags: 0 @@ -377,6 +320,63 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1001 &838571664 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 4230525904207381876, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_Name + value: Unit (1) + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_RootOrder + value: 5 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalPosition.x + value: 0.27 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalPosition.y + value: 0.35 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 4230525904207381882, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: a8bd984924ce1094ca9ddfae49c14cfc, type: 3} --- !u!1 &1178957205 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Unit.cs b/Assets/Scripts/Unit.cs index ab88624..3e77897 100644 --- a/Assets/Scripts/Unit.cs +++ b/Assets/Scripts/Unit.cs @@ -4,8 +4,12 @@ public class Unit : MonoBehaviour { [SerializeField] private GameObject selectedIndicator; + private UnitMovement unitMovement; + private void Awake() { + unitMovement = GetComponent(); + Deselect(); } @@ -18,4 +22,9 @@ public void Deselect() { selectedIndicator.SetActive(false); } + + public void MoveTo(Vector3 targetPosition) + { + unitMovement.SetMovePosition(targetPosition); + } } diff --git a/Assets/Scripts/UnitSelector.cs b/Assets/Scripts/UnitController.cs similarity index 69% rename from Assets/Scripts/UnitSelector.cs rename to Assets/Scripts/UnitController.cs index 3eeaa29..3200fc1 100644 --- a/Assets/Scripts/UnitSelector.cs +++ b/Assets/Scripts/UnitController.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using UnityEngine; -public class UnitSelector : MonoBehaviour +public class UnitController : MonoBehaviour { [SerializeField] private Transform selectionArea; @@ -32,6 +32,18 @@ private void Update() { SelectUnits(); } + + if (Input.GetMouseButton(1)) + { + List targetPositions = GetTargetPositions(); + int targetIndex = 0; + + foreach (Unit unit in selectedUnits) + { + unit.MoveTo(targetPositions[targetIndex]); + targetIndex++; + } + } } private void StartSelection() @@ -89,4 +101,30 @@ private Vector3 GetMouseWorldPosition() return worldPosition; } + + private List GetTargetPositions() + { + List targetPositions = new List(); + int multiplier = selectedUnits.Count / -2; + Vector3 centre = GetMouseWorldPosition(); + int x = 0; + + if (selectedUnits.Count == 1 || selectedUnits.Count % 2 != 0) + { + x = 10 * multiplier; + } + else + { + x = 10 * multiplier + 5; + } + + for (int i = 0; i < selectedUnits.Count; i++) + { + targetPositions.Add(new Vector3(centre.x + x, centre.y)); + + x += 10; + } + + return targetPositions; + } } diff --git a/Assets/Scripts/UnitSelector.cs.meta b/Assets/Scripts/UnitController.cs.meta similarity index 100% rename from Assets/Scripts/UnitSelector.cs.meta rename to Assets/Scripts/UnitController.cs.meta diff --git a/Assets/Scripts/UnitMovement.cs b/Assets/Scripts/UnitMovement.cs new file mode 100644 index 0000000..804b378 --- /dev/null +++ b/Assets/Scripts/UnitMovement.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class UnitMovement : MonoBehaviour +{ + [SerializeField] private float speed = 1f; + + private Rigidbody2D rigidbody2d; + private Vector3 moveDirection; + private Vector3 movePosition; + + private void Awake() + { + rigidbody2d = GetComponent(); + movePosition = transform.position; + } + + // Update is called once per frame + private void Update() + { + moveDirection = (movePosition - transform.position).normalized; + + if (Vector3.Distance(movePosition, transform.position) < 0f) + { + moveDirection = Vector3.zero; + } + } + + private void FixedUpdate() + { + rigidbody2d.velocity = moveDirection * speed; + } + + public void SetMovePosition(Vector3 movePosition) + { + this.movePosition = movePosition; + } +} diff --git a/Assets/Scripts/UnitMovement.cs.meta b/Assets/Scripts/UnitMovement.cs.meta new file mode 100644 index 0000000..c69257e --- /dev/null +++ b/Assets/Scripts/UnitMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 87670e1830ba2774bb8e7efda1a9a330 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UserSettings/Layouts/default-2021.dwlt b/UserSettings/Layouts/default-2021.dwlt index 27599cf..c2a514d 100644 --- a/UserSettings/Layouts/default-2021.dwlt +++ b/UserSettings/Layouts/default-2021.dwlt @@ -19,7 +19,7 @@ MonoBehaviour: width: 1280 height: 629.3334 m_ShowMode: 4 - m_Title: Hierarchy + m_Title: Inspector m_RootView: {fileID: 6} m_MinSize: {x: 875, y: 300} m_MaxSize: {x: 10000, y: 10000} @@ -48,7 +48,7 @@ MonoBehaviour: m_MinSize: {x: 300, y: 200} m_MaxSize: {x: 24288, y: 16192} vertical: 0 - controlID: 356 + controlID: 56 --- !u!114 &3 MonoBehaviour: m_ObjectHideFlags: 52 @@ -68,8 +68,8 @@ MonoBehaviour: y: 0 width: 302 height: 579.3334 - m_MinSize: {x: 276, y: 71} - m_MaxSize: {x: 4001, y: 4021} + m_MinSize: {x: 275, y: 50} + m_MaxSize: {x: 4000, y: 4000} m_ActualView: {fileID: 13} m_Panes: - {fileID: 13} @@ -94,8 +94,8 @@ MonoBehaviour: y: 0 width: 242 height: 343.33334 - m_MinSize: {x: 201, y: 221} - m_MaxSize: {x: 4001, y: 4021} + m_MinSize: {x: 200, y: 200} + m_MaxSize: {x: 4000, y: 4000} m_ActualView: {fileID: 14} m_Panes: - {fileID: 14} @@ -111,7 +111,7 @@ MonoBehaviour: m_Enabled: 1 m_EditorHideFlags: 1 m_Script: {fileID: 12006, guid: 0000000000000000e000000000000000, type: 0} - m_Name: ProjectBrowser + m_Name: ConsoleWindow m_EditorClassIdentifier: m_Children: [] m_Position: @@ -120,14 +120,14 @@ MonoBehaviour: y: 343.33334 width: 978 height: 236.00003 - m_MinSize: {x: 231, y: 271} - m_MaxSize: {x: 10001, y: 10021} - m_ActualView: {fileID: 12} + m_MinSize: {x: 100, y: 100} + m_MaxSize: {x: 4000, y: 4000} + m_ActualView: {fileID: 17} m_Panes: - {fileID: 12} - {fileID: 17} - m_Selected: 0 - m_LastSelected: 1 + m_Selected: 1 + m_LastSelected: 0 --- !u!114 &6 MonoBehaviour: m_ObjectHideFlags: 52 @@ -223,7 +223,7 @@ MonoBehaviour: m_MinSize: {x: 200, y: 200} m_MaxSize: {x: 16192, y: 16192} vertical: 1 - controlID: 357 + controlID: 57 --- !u!114 &10 MonoBehaviour: m_ObjectHideFlags: 52 @@ -248,7 +248,7 @@ MonoBehaviour: m_MinSize: {x: 200, y: 100} m_MaxSize: {x: 16192, y: 8096} vertical: 0 - controlID: 358 + controlID: 58 --- !u!114 &11 MonoBehaviour: m_ObjectHideFlags: 52 @@ -299,7 +299,7 @@ MonoBehaviour: x: 0 y: 416 width: 977 - height: 215.00003 + height: 214.99997 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -317,22 +317,22 @@ MonoBehaviour: m_SkipHidden: 0 m_SearchArea: 1 m_Folders: - - Assets/Prefabs + - Assets/Scripts m_Globs: [] m_OriginalText: m_ViewMode: 1 m_StartGridSize: 16 m_LastFolders: - - Assets/Prefabs + - Assets/Scripts m_LastFoldersGridSize: 16 m_LastProjectPath: D:\GitHub\battle-simulator m_LockTracker: m_IsLocked: 0 m_FolderTreeState: scrollPos: {x: 0, y: 0} - m_SelectedIDs: 84720000 - m_LastClickedID: 29316 - m_ExpandedIDs: 00000000fe5b000000ca9a3bffffff7f + m_SelectedIDs: 6e5d0000 + m_LastClickedID: 23918 + m_ExpandedIDs: 000000005e5d0000 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -360,7 +360,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 00000000fe5b000000ca9a3bffffff7f + m_ExpandedIDs: 000000005e5d0000 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -488,9 +488,9 @@ MonoBehaviour: m_SceneHierarchy: m_TreeViewState: scrollPos: {x: 0, y: 0} - m_SelectedIDs: - m_LastClickedID: 0 - m_ExpandedIDs: 98faffff345d0000505d0000 + m_SelectedIDs: 14fbffff + m_LastClickedID: -1260 + m_ExpandedIDs: 2afbffff m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -547,8 +547,8 @@ MonoBehaviour: floating: 0 collapsed: 0 displayed: 1 - snapOffset: {x: 0, y: 0} - snapOffsetDelta: {x: -98.66669, y: -26} + snapOffset: {x: -101, y: -26} + snapOffsetDelta: {x: 0, y: 0} snapCorner: 3 id: Tool Settings index: 0 @@ -558,9 +558,9 @@ MonoBehaviour: floating: 0 collapsed: 0 displayed: 1 - snapOffset: {x: -141, y: 149} + snapOffset: {x: -141, y: -173} snapOffsetDelta: {x: 0, y: 0} - snapCorner: 1 + snapCorner: 3 id: unity-grid-and-snap-toolbar index: 1 layout: 1 @@ -908,7 +908,7 @@ MonoBehaviour: m_ShowGizmos: 0 m_TargetDisplay: 0 m_ClearColor: {r: 0, g: 0, b: 0, a: 0} - m_TargetSize: {x: 734, y: 301.33334} + m_TargetSize: {x: 536, y: 301.33334} m_TextureFilterMode: 0 m_TextureHideFlags: 61 m_RenderIMGUI: 1 @@ -917,14 +917,14 @@ MonoBehaviour: m_VSyncEnabled: 0 m_Gizmos: 0 m_Stats: 0 - m_SelectedSizes: 00000000000000000000000000000000000000000000000000000000000000000000000000000000 + m_SelectedSizes: 01000000000000000000000000000000000000000000000000000000000000000000000000000000 m_ZoomArea: m_HRangeLocked: 0 m_VRangeLocked: 0 hZoomLockedByDefault: 0 vZoomLockedByDefault: 0 - m_HBaseRangeMin: -244.66667 - m_HBaseRangeMax: 244.66667 + m_HBaseRangeMin: -178.66667 + m_HBaseRangeMax: 178.66667 m_VBaseRangeMin: -100.44445 m_VBaseRangeMax: 100.44445 m_HAllowExceedBaseRangeMin: 1