Added better movement and renamed

This commit is contained in:
BOT Alex 2023-08-20 03:31:31 +02:00
parent 54e32060f1
commit d4dd41ff9b
4 changed files with 41 additions and 63 deletions

View File

@ -563,10 +563,10 @@ TilemapCollider2D:
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 0
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 1
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
m_MaximumTileChangeCount: 1000 m_MaximumTileChangeCount: 1000
m_ExtrusionFactor: 0 m_ExtrusionFactor: 0.01
m_UseDelaunayMesh: 0 m_UseDelaunayMesh: 0
--- !u!1 &772028196 --- !u!1 &772028196
GameObject: GameObject:
@ -1357,10 +1357,10 @@ TilemapCollider2D:
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 0
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 1
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
m_MaximumTileChangeCount: 1000 m_MaximumTileChangeCount: 1000
m_ExtrusionFactor: 0 m_ExtrusionFactor: 0.01
m_UseDelaunayMesh: 0 m_UseDelaunayMesh: 0
--- !u!1 &2123771200 --- !u!1 &2123771200
GameObject: GameObject:
@ -1372,11 +1372,11 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 2123771202} - component: {fileID: 2123771202}
- component: {fileID: 2123771201} - component: {fileID: 2123771201}
- component: {fileID: 2123771205}
- component: {fileID: 2123771204} - component: {fileID: 2123771204}
- component: {fileID: 2123771203} - component: {fileID: 2123771203}
- component: {fileID: 2123771205}
m_Layer: 0 m_Layer: 0
m_Name: Idle 1 m_Name: Player
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -1464,13 +1464,9 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
moveSpeed: 2000 moveSpeed: 2000
jumpForce: 10 jumpForce: 7
maxSpeed: 5 isGrounded: 0
jumpTime: 0 maxSpeed: 7
groundCheck: {fileID: 772028197}
groundLayer:
serializedVersion: 2
m_Bits: 8
--- !u!50 &2123771204 --- !u!50 &2123771204
Rigidbody2D: Rigidbody2D:
serializedVersion: 4 serializedVersion: 4
@ -1541,5 +1537,5 @@ BoxCollider2D:
adaptiveTiling: 0 adaptiveTiling: 0
m_AutoTiling: 0 m_AutoTiling: 0
serializedVersion: 2 serializedVersion: 2
m_Size: {x: 0.9, y: 0.9} m_Size: {x: 0.5, y: 0.5}
m_EdgeRadius: 0 m_EdgeRadius: 0.25

View File

@ -2,7 +2,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class LoadFromSave : MonoBehaviour public class JsonManager : MonoBehaviour
{ {
public void LoadChar() public void LoadChar()

View File

@ -1,68 +1,50 @@
using Unity.Mathematics;
using UnityEngine; using UnityEngine;
public class PlayerMovement : MonoBehaviour public class PlayerMovement : MonoBehaviour
{ {
public float moveSpeed = 5f; Vector2 spawnedPosition;
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;
private Rigidbody2D rb; public float moveSpeed = 500f;
private bool isGrounded; public float jumpForce = 10f; // The force applied when jumping
private bool isJumping; public bool isGrounded = false; // To check if the character is on the ground
private float jumpTimeCounter; Rigidbody2D rb;
private void Awake() public float maxSpeed = 5f;
// Start is called before the first frame update
void Start()
{ {
rb = GetComponent<Rigidbody2D>(); rb = GetComponent<Rigidbody2D>();
spawnedPosition = transform.position;
} }
private void Update() // Update is called once per frame
void Update()
{ {
// Check if the player is grounded // Check for ground contact using raycast
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer); isGrounded = true;
// Movement // Resets the player pos
float moveDirection = Input.GetAxis("Horizontal"); if (Input.GetKeyDown(KeyCode.Backspace))
Vector2 movement = new Vector2(moveDirection, 0f);
// Apply speed cap
if (Mathf.Abs(rb.velocity.x) > maxSpeed)
{ {
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 // Jumping
if (isGrounded) if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{ {
if (Input.GetKeyDown(KeyCode.Space)) rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
} }
if (Input.GetKey(KeyCode.Space) && isJumping) // Clamps speed
{ if (math.abs(rb.velocity.x) > maxSpeed)
if (jumpTimeCounter > 0) rb.velocity = new Vector2((rb.velocity.x / math.abs(rb.velocity.x)) * maxSpeed, rb.velocity.y);
{
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);
} }
} }