From 4d9862a2b09ede3de5bbcaeaebb6e4c2117455b5 Mon Sep 17 00:00:00 2001 From: KerelOlivier Date: Sat, 2 Apr 2022 12:14:48 +0200 Subject: [PATCH] add: player movement --- .idea/.idea.LD50/.idea/.gitignore | 13 ++ .idea/.idea.LD50/.idea/encodings.xml | 4 + .idea/.idea.LD50/.idea/indexLayout.xml | 8 ++ .idea/.idea.LD50/.idea/vcs.xml | 6 + Assets/Input.meta | 8 ++ Assets/Input/Player.inputactions | 109 ++++++++++++++++ Assets/Input/Player.inputactions.meta | 14 +++ Assets/Scenes/test.unity | 157 +++++++++++++++++++++++- Assets/Scripts/PlayerController.cs | 53 ++++++++ Assets/Scripts/PlayerController.cs.meta | 11 ++ Assets/Sprites/Player.png | Bin 0 -> 362 bytes Assets/Sprites/Player.png.meta | 120 ++++++++++++++++++ Packages/manifest.json | 1 + Packages/packages-lock.json | 9 ++ ProjectSettings/ProjectSettings.asset | 2 +- 15 files changed, 513 insertions(+), 2 deletions(-) create mode 100644 .idea/.idea.LD50/.idea/.gitignore create mode 100644 .idea/.idea.LD50/.idea/encodings.xml create mode 100644 .idea/.idea.LD50/.idea/indexLayout.xml create mode 100644 .idea/.idea.LD50/.idea/vcs.xml create mode 100644 Assets/Input.meta create mode 100644 Assets/Input/Player.inputactions create mode 100644 Assets/Input/Player.inputactions.meta create mode 100644 Assets/Scripts/PlayerController.cs create mode 100644 Assets/Scripts/PlayerController.cs.meta create mode 100644 Assets/Sprites/Player.png create mode 100644 Assets/Sprites/Player.png.meta diff --git a/.idea/.idea.LD50/.idea/.gitignore b/.idea/.idea.LD50/.idea/.gitignore new file mode 100644 index 0000000..4ada262 --- /dev/null +++ b/.idea/.idea.LD50/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/.idea.LD50.iml +/contentModel.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.LD50/.idea/encodings.xml b/.idea/.idea.LD50/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.LD50/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.LD50/.idea/indexLayout.xml b/.idea/.idea.LD50/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.LD50/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.LD50/.idea/vcs.xml b/.idea/.idea.LD50/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.LD50/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assets/Input.meta b/Assets/Input.meta new file mode 100644 index 0000000..a2d19ba --- /dev/null +++ b/Assets/Input.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0366a53eb4119ab5ba6501929f29eef6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Input/Player.inputactions b/Assets/Input/Player.inputactions new file mode 100644 index 0000000..d5a3c90 --- /dev/null +++ b/Assets/Input/Player.inputactions @@ -0,0 +1,109 @@ +{ + "name": "Player", + "maps": [ + { + "name": "default", + "id": "83ceaef8-7a9f-40f7-8e71-469b434b3d9a", + "actions": [ + { + "name": "Move", + "type": "Value", + "id": "1163ce9d-c7cb-41b7-b122-5360af9d9773", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "Fight", + "type": "Button", + "id": "9b6e6f96-096c-4d0e-ad98-05be447902a7", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + } + ], + "bindings": [ + { + "name": "WASD", + "id": "47d8955c-bf93-4735-804c-3b7dc82985c8", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "949f9fa2-bf12-4f98-a1ec-4ca563c0566a", + "path": "/w", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "d9753777-7aaf-4e86-bbbc-a49cd33935dc", + "path": "/s", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "298f5cd4-04fe-4b2f-bd82-c1536d59658b", + "path": "/a", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "373de430-6124-4b20-a2c1-e3f0704857ba", + "path": "/d", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "", + "id": "d6733eac-0ba0-49b9-af28-85a00c268050", + "path": "/space", + "interactions": "", + "processors": "", + "groups": "", + "action": "Fight", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "c9b00421-6536-4edf-96cf-4e4f19a30c1b", + "path": "/leftButton", + "interactions": "", + "processors": "", + "groups": "", + "action": "Fight", + "isComposite": false, + "isPartOfComposite": false + } + ] + } + ], + "controlSchemes": [] +} \ No newline at end of file diff --git a/Assets/Input/Player.inputactions.meta b/Assets/Input/Player.inputactions.meta new file mode 100644 index 0000000..2b718ba --- /dev/null +++ b/Assets/Input/Player.inputactions.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: 072cf7ce024a1a2ee8707f72b713be30 +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3} + generateWrapperCode: 0 + wrapperCodePath: + wrapperClassName: + wrapperCodeNamespace: diff --git a/Assets/Scenes/test.unity b/Assets/Scenes/test.unity index 8eec3f5..4c7ee8f 100644 --- a/Assets/Scenes/test.unity +++ b/Assets/Scenes/test.unity @@ -132,6 +132,11 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 394882246} + - component: {fileID: 394882247} + - component: {fileID: 394882248} + - component: {fileID: 394882250} + - component: {fileID: 394882249} + - component: {fileID: 394882251} m_Layer: 0 m_Name: Player m_TagString: Untagged @@ -153,6 +158,156 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &394882247 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 394882245} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: ea7869a6d259396a0a452fe8ff118af0, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!114 &394882248 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 394882245} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Actions: {fileID: -944628639613478452, guid: 072cf7ce024a1a2ee8707f72b713be30, type: 3} + m_NotificationBehavior: 2 + m_UIInputModule: {fileID: 0} + m_DeviceLostEvent: + m_PersistentCalls: + m_Calls: [] + m_DeviceRegainedEvent: + m_PersistentCalls: + m_Calls: [] + m_ControlsChangedEvent: + m_PersistentCalls: + m_Calls: [] + m_ActionEvents: + - m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 0} + m_TargetAssemblyTypeName: + m_MethodName: + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + m_ActionId: 1163ce9d-c7cb-41b7-b122-5360af9d9773 + m_ActionName: default/Movement[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d] + - m_PersistentCalls: + m_Calls: [] + m_ActionId: 9b6e6f96-096c-4d0e-ad98-05be447902a7 + m_ActionName: default/Fight[/Keyboard/space,/Mouse/leftButton] + m_NeverAutoSwitchControlSchemes: 0 + m_DefaultControlScheme: + m_DefaultActionMap: default + m_SplitScreenIndex: -1 + m_Camera: {fileID: 0} +--- !u!58 &394882249 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 394882245} + 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.37 +--- !u!50 &394882250 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 394882245} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 0.0001 + 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: 0 +--- !u!114 &394882251 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 394882245} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ff7058e4b6f609474b877ecc3d8c9e62, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &519420028 GameObject: m_ObjectHideFlags: 0 @@ -189,7 +344,7 @@ Camera: m_Enabled: 1 serializedVersion: 2 m_ClearFlags: 2 - m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_BackGroundColor: {r: 0.4245283, g: 0.4245283, b: 0.4245283, a: 0} m_projectionMatrixMode: 1 m_GateFitMode: 2 m_FOVAxisMode: 0 diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs new file mode 100644 index 0000000..34f6de5 --- /dev/null +++ b/Assets/Scripts/PlayerController.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + +public class PlayerController : MonoBehaviour +{ + private Rigidbody2D rb; + private SpriteRenderer sr; + private Camera main; + + //Input + private PlayerInput input; + private InputAction moveAction; + private InputAction fightAction; + + private Vector2 moveDirection; + private float moveSpeed = 5f; + + + // Start is called before the first frame update + private void Awake() + { + rb = this.gameObject.GetComponent(); + input = this.gameObject.GetComponent(); + moveAction = input.actions["Move"]; + fightAction = input.actions["Fight"]; + + //Input events + moveAction.performed += ctx => Move(ctx.ReadValue()); + moveAction.canceled += ctx => Move(Vector2.zero); + main = Camera.main; + } + + private void FixedUpdate() + { + //Update move direction + rb.MovePosition(rb.position + moveDirection * moveSpeed * Time.fixedDeltaTime); + } + + private void Update() + { + //Update the sprite direction based on mouse position + Vector2 mousePos = main.ScreenToWorldPoint(Mouse.current.position.ReadValue()); + Vector2 direction = mousePos - (Vector2)transform.position; + float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; + transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); + } + + //update the move direction + void Move(Vector2 direction) => moveDirection = direction; +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerController.cs.meta b/Assets/Scripts/PlayerController.cs.meta new file mode 100644 index 0000000..f6fc0bb --- /dev/null +++ b/Assets/Scripts/PlayerController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ff7058e4b6f609474b877ecc3d8c9e62 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Sprites/Player.png b/Assets/Sprites/Player.png new file mode 100644 index 0000000000000000000000000000000000000000..f3a60f40e36361fcc24a0093564ab1f580d95b13 GIT binary patch literal 362 zcmV-w0hRuVP)Px$BuPX;RCt{2+1n9R<6ji%wZ2A{JoLcIXZ@KJ7PXceJafJOL}MG2Wm#is5t$?$IPc0SUqJSt@#I62ev@?z0C(2a;Irr8F zP*ZLQxG12?O;F@~H^Mc4gNLtxFobUJl$Vt)4?>q30?i+VMgH$)NaYG>H4IMkIxND3 zh=@3!5{q0pF literal 0 HcmV?d00001 diff --git a/Assets/Sprites/Player.png.meta b/Assets/Sprites/Player.png.meta new file mode 100644 index 0000000..bf44b12 --- /dev/null +++ b/Assets/Sprites/Player.png.meta @@ -0,0 +1,120 @@ +fileFormatVersion: 2 +guid: ea7869a6d259396a0a452fe8ff118af0 +TextureImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 11 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 0 + aniso: 1 + mipBias: 0 + wrapU: 1 + wrapV: 1 + wrapW: 1 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 1 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 64 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 8 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: WebGL + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: 5e97eb03825dee720800000000000000 + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spritePackingTag: + pSDRemoveMatte: 0 + pSDShowRemoveMatteOption: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/manifest.json b/Packages/manifest.json index e960955..aecda00 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -10,6 +10,7 @@ "com.unity.ide.rider": "2.0.7", "com.unity.ide.visualstudio": "2.0.14", "com.unity.ide.vscode": "1.2.5", + "com.unity.inputsystem": "1.3.0", "com.unity.test-framework": "1.1.31", "com.unity.textmeshpro": "3.0.6", "com.unity.timeline": "1.4.8", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index fd552cb..cf13716 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -113,6 +113,15 @@ "dependencies": {}, "url": "https://packages.unity.com" }, + "com.unity.inputsystem": { + "version": "1.3.0", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.modules.uielements": "1.0.0" + }, + "url": "https://packages.unity.com" + }, "com.unity.mathematics": { "version": "1.1.0", "depth": 1, diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index bc2e4b9..42d117a 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -650,7 +650,7 @@ PlayerSettings: m_VersionCode: 1 m_VersionName: apiCompatibilityLevel: 6 - activeInputHandler: 0 + activeInputHandler: 1 cloudProjectId: framebufferDepthMemorylessMode: 0 qualitySettingsNames: []