From d4dd41ff9b922c3e7916ed9b536cf290fc336648 Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Sun, 20 Aug 2023 03:31:31 +0200 Subject: [PATCH] Added better movement and renamed --- Assets/Scenes/Level1.unity | 26 +++---- .../{LoadFromSave.cs => JsonManager.cs} | 2 +- ...adFromSave.cs.meta => JsonManager.cs.meta} | 0 Assets/Scripts/PlayerMovement.cs | 76 +++++++------------ 4 files changed, 41 insertions(+), 63 deletions(-) rename Assets/Scripts/{LoadFromSave.cs => JsonManager.cs} (81%) rename Assets/Scripts/{LoadFromSave.cs.meta => JsonManager.cs.meta} (100%) diff --git a/Assets/Scenes/Level1.unity b/Assets/Scenes/Level1.unity index 33454b4..4bfb3cf 100644 --- a/Assets/Scenes/Level1.unity +++ b/Assets/Scenes/Level1.unity @@ -563,10 +563,10 @@ TilemapCollider2D: m_Bits: 4294967295 m_IsTrigger: 0 m_UsedByEffector: 0 - m_UsedByComposite: 0 + m_UsedByComposite: 1 m_Offset: {x: 0, y: 0} m_MaximumTileChangeCount: 1000 - m_ExtrusionFactor: 0 + m_ExtrusionFactor: 0.01 m_UseDelaunayMesh: 0 --- !u!1 &772028196 GameObject: @@ -1357,10 +1357,10 @@ TilemapCollider2D: m_Bits: 4294967295 m_IsTrigger: 0 m_UsedByEffector: 0 - m_UsedByComposite: 0 + m_UsedByComposite: 1 m_Offset: {x: 0, y: 0} m_MaximumTileChangeCount: 1000 - m_ExtrusionFactor: 0 + m_ExtrusionFactor: 0.01 m_UseDelaunayMesh: 0 --- !u!1 &2123771200 GameObject: @@ -1372,11 +1372,11 @@ GameObject: m_Component: - component: {fileID: 2123771202} - component: {fileID: 2123771201} - - component: {fileID: 2123771205} - component: {fileID: 2123771204} - component: {fileID: 2123771203} + - component: {fileID: 2123771205} m_Layer: 0 - m_Name: Idle 1 + m_Name: Player m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -1464,13 +1464,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: moveSpeed: 2000 - jumpForce: 10 - maxSpeed: 5 - jumpTime: 0 - groundCheck: {fileID: 772028197} - groundLayer: - serializedVersion: 2 - m_Bits: 8 + jumpForce: 7 + isGrounded: 0 + maxSpeed: 7 --- !u!50 &2123771204 Rigidbody2D: serializedVersion: 4 @@ -1541,5 +1537,5 @@ BoxCollider2D: adaptiveTiling: 0 m_AutoTiling: 0 serializedVersion: 2 - m_Size: {x: 0.9, y: 0.9} - m_EdgeRadius: 0 + m_Size: {x: 0.5, y: 0.5} + m_EdgeRadius: 0.25 diff --git a/Assets/Scripts/LoadFromSave.cs b/Assets/Scripts/JsonManager.cs similarity index 81% rename from Assets/Scripts/LoadFromSave.cs rename to Assets/Scripts/JsonManager.cs index 11f560c..729d888 100644 --- a/Assets/Scripts/LoadFromSave.cs +++ b/Assets/Scripts/JsonManager.cs @@ -2,7 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class LoadFromSave : MonoBehaviour +public class JsonManager : MonoBehaviour { public void LoadChar() diff --git a/Assets/Scripts/LoadFromSave.cs.meta b/Assets/Scripts/JsonManager.cs.meta similarity index 100% rename from Assets/Scripts/LoadFromSave.cs.meta rename to Assets/Scripts/JsonManager.cs.meta diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index a54bd19..0536da2 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -1,68 +1,50 @@ +using Unity.Mathematics; using UnityEngine; public class PlayerMovement : MonoBehaviour { - public float moveSpeed = 5f; - public float jumpForce = 10f; - public float maxSpeed = 10f; - public float jumpTime = 0.5f; // Maximum time the jump key can be held - public Transform groundCheck; - public LayerMask groundLayer; + Vector2 spawnedPosition; - private Rigidbody2D rb; - private bool isGrounded; - private bool isJumping; - private float jumpTimeCounter; + public float moveSpeed = 500f; + public float jumpForce = 10f; // The force applied when jumping + public bool isGrounded = false; // To check if the character is on the ground + Rigidbody2D rb; - private void Awake() + public float maxSpeed = 5f; + + // Start is called before the first frame update + void Start() { rb = GetComponent(); + spawnedPosition = transform.position; } - private void Update() + // Update is called once per frame + void Update() { - // Check if the player is grounded - isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer); + // Check for ground contact using raycast + isGrounded = true; - // Movement - float moveDirection = Input.GetAxis("Horizontal"); - Vector2 movement = new Vector2(moveDirection, 0f); - - // Apply speed cap - if (Mathf.Abs(rb.velocity.x) > maxSpeed) + // Resets the player pos + if (Input.GetKeyDown(KeyCode.Backspace)) { - movement.x = 0f; + transform.position = spawnedPosition; + rb.velocity = Vector3.zero; } + if (Input.GetKey(KeyCode.A)) + rb.AddForce(Vector2.left * (Time.deltaTime * moveSpeed)); + if (Input.GetKey(KeyCode.D)) + rb.AddForce(Vector2.right * (Time.deltaTime * moveSpeed)); + // Jumping - if (isGrounded) + if (isGrounded && Input.GetKeyDown(KeyCode.Space)) { - if (Input.GetKeyDown(KeyCode.Space)) - { - isJumping = true; - jumpTimeCounter = jumpTime; - rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); - } + rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); } - if (Input.GetKey(KeyCode.Space) && isJumping) - { - if (jumpTimeCounter > 0) - { - rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Force); - jumpTimeCounter -= Time.deltaTime; - } - else - { - isJumping = false; - } - } - - if (Input.GetKeyUp(KeyCode.Space)) - { - isJumping = false; - } - - rb.AddForce(movement * moveSpeed, ForceMode2D.Force); + // Clamps speed + if (math.abs(rb.velocity.x) > maxSpeed) + rb.velocity = new Vector2((rb.velocity.x / math.abs(rb.velocity.x)) * maxSpeed, rb.velocity.y); } }