Added better movement and renamed
This commit is contained in:
parent
54e32060f1
commit
d4dd41ff9b
|
@ -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
|
||||
|
|
|
@ -2,7 +2,7 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class LoadFromSave : MonoBehaviour
|
||||
public class JsonManager : MonoBehaviour
|
||||
{
|
||||
|
||||
public void LoadChar()
|
|
@ -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<Rigidbody2D>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue