mirror of
https://github.com/DerTyp7/example-plattformer-unity.git
synced 2025-10-29 12:32:12 +01:00
new movement
This commit is contained in:
@@ -655,13 +655,8 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: bca6c51a74bcf0c40a337fcb0ec638a4, type: 3}
|
m_Script: {fileID: 11500000, guid: bca6c51a74bcf0c40a337fcb0ec638a4, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
speed: 7
|
|
||||||
jumpForce: 10
|
|
||||||
jumpTime: 0.2
|
|
||||||
coyoteJumpTime: 0.2
|
|
||||||
groundCheck: {fileID: 782312216}
|
groundCheck: {fileID: 782312216}
|
||||||
groundCheckRadius: 0.2
|
groundLayer:
|
||||||
groundLayerMask:
|
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
m_Bits: 64
|
m_Bits: 64
|
||||||
--- !u!61 &1301724320
|
--- !u!61 &1301724320
|
||||||
|
|||||||
@@ -1,110 +1,96 @@
|
|||||||
|
using System.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class PlayerMovement : MonoBehaviour
|
public class PlayerMovement : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] private float speed;
|
private float horizontal;
|
||||||
private Rigidbody2D rb;
|
private float speed = 8f;
|
||||||
private float horizontalMovementInput;
|
private float jumpingPower = 16f;
|
||||||
private SpriteRenderer spriteRenderer;
|
private bool isFacingRight = true;
|
||||||
|
|
||||||
//* Jump
|
|
||||||
[Header("Jump")]
|
|
||||||
[SerializeField] private float jumpForce;
|
|
||||||
[SerializeField] private float jumpTime;
|
|
||||||
private float jumpTimeCounter;
|
|
||||||
private bool isJumping;
|
private bool isJumping;
|
||||||
|
|
||||||
//* Coyote Jumping
|
private float coyoteTime = 0.2f;
|
||||||
[Header("Coyote Jumping")]
|
private float coyoteTimeCounter;
|
||||||
[SerializeField] private float coyoteJumpTime;
|
|
||||||
private float coyoteJumpTimeCounter;
|
|
||||||
//* Ground Check
|
|
||||||
[Header("Ground Check")]
|
|
||||||
[SerializeField] private Transform groundCheck;
|
|
||||||
[SerializeField] private float groundCheckRadius;
|
|
||||||
[SerializeField] private LayerMask groundLayerMask;
|
|
||||||
private bool isGrounded;
|
|
||||||
|
|
||||||
|
private float jumpBufferTime = 0.2f;
|
||||||
|
private float jumpBufferCounter;
|
||||||
|
|
||||||
|
private Rigidbody2D rb;
|
||||||
|
[SerializeField] private Transform groundCheck;
|
||||||
|
[SerializeField] private LayerMask groundLayer;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
rb = GetComponent<Rigidbody2D>();
|
rb = GetComponent<Rigidbody2D>();
|
||||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
Debug.DrawLine(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), Color.red, 0f);
|
horizontal = Input.GetAxisRaw("Horizontal");
|
||||||
|
|
||||||
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayerMask);
|
if (IsGrounded())
|
||||||
horizontalMovementInput = Input.GetAxisRaw("Horizontal");
|
{
|
||||||
|
coyoteTimeCounter = coyoteTime;
|
||||||
HandleCoyoteJump();
|
|
||||||
HandleSpriteDirection();
|
|
||||||
HandleJump();
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
coyoteTimeCounter -= Time.deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetButtonDown("Jump"))
|
||||||
|
{
|
||||||
|
jumpBufferCounter = jumpBufferTime;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
jumpBufferCounter -= Time.deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (coyoteTimeCounter > 0f && jumpBufferCounter > 0f && !isJumping)
|
||||||
|
{
|
||||||
|
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
|
||||||
|
|
||||||
|
jumpBufferCounter = 0f;
|
||||||
|
|
||||||
|
StartCoroutine(JumpCooldown());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
|
||||||
|
{
|
||||||
|
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
|
||||||
|
|
||||||
|
coyoteTimeCounter = 0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
Flip();
|
||||||
|
}
|
||||||
|
|
||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
{
|
{
|
||||||
rb.velocity = new Vector2(horizontalMovementInput * speed, rb.velocity.y);
|
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleSpriteDirection()
|
private bool IsGrounded()
|
||||||
{
|
{
|
||||||
if (horizontalMovementInput > 0)
|
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
|
||||||
{
|
|
||||||
spriteRenderer.flipX = false;
|
|
||||||
}
|
}
|
||||||
else if (horizontalMovementInput < 0)
|
|
||||||
|
private void Flip()
|
||||||
{
|
{
|
||||||
spriteRenderer.flipX = true;
|
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
|
||||||
|
{
|
||||||
|
Vector3 localScale = transform.localScale;
|
||||||
|
isFacingRight = !isFacingRight;
|
||||||
|
localScale.x *= -1f;
|
||||||
|
transform.localScale = localScale;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleJump()
|
private IEnumerator JumpCooldown()
|
||||||
{
|
|
||||||
if (Input.GetButtonDown("Jump") && coyoteJumpTimeCounter > 0)
|
|
||||||
{
|
{
|
||||||
isJumping = true;
|
isJumping = true;
|
||||||
jumpTimeCounter = jumpTime;
|
yield return new WaitForSeconds(0.4f);
|
||||||
rb.velocity = Vector2.up * jumpForce;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Input.GetButton("Jump") && isJumping)
|
|
||||||
{
|
|
||||||
if (jumpTimeCounter > 0)
|
|
||||||
{
|
|
||||||
jumpTimeCounter -= Time.deltaTime;
|
|
||||||
rb.velocity = Vector2.up * jumpForce;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
isJumping = false;
|
isJumping = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.GetButtonUp("Jump"))
|
|
||||||
{
|
|
||||||
isJumping = false;
|
|
||||||
coyoteJumpTimeCounter = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleCoyoteJump()
|
|
||||||
{
|
|
||||||
if (isGrounded)
|
|
||||||
{
|
|
||||||
coyoteJumpTimeCounter = coyoteJumpTime;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
coyoteJumpTimeCounter -= Time.deltaTime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw Gizmos-Sphere
|
|
||||||
private void OnDrawGizmos()
|
|
||||||
{
|
|
||||||
Gizmos.color = Color.red;
|
|
||||||
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user