Lost_in_the_Nyadows/Assets/Scripts/PlayerMovement.cs

116 lines
3.3 KiB
C#
Raw Normal View History

2025-02-18 16:06:31 +01:00
using Unity.VisualScripting.FullSerializer;
2025-03-03 21:58:44 +01:00
using UnityEditor.Experimental.GraphView;
2025-02-18 16:06:31 +01:00
using UnityEngine;
2025-03-03 21:58:44 +01:00
/// <summary>
/// Playermovement, sowie Animation usw. hier angeschaut: https://www.youtube.com/playlist?list=PLgOEwFbvGm5o8hayFB6skAfa8Z-mw4dPV
/// </summary>
2025-02-18 16:06:31 +01:00
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
2025-03-03 21:58:44 +01:00
[SerializeField] private float jumpPower;
2025-02-18 16:06:31 +01:00
[SerializeField] private LayerMask groundLayer;
[SerializeField] private LayerMask wallLayer;
private Rigidbody2D body;
private Animator anim;
private BoxCollider2D boxCollider;
private float wallJumpCooldown;
2025-03-03 21:58:44 +01:00
private float horizontalInput;
2025-02-18 16:06:31 +01:00
private void Awake()
{
// grab references for rigidbody & animatior from gameobject
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
2025-03-03 21:58:44 +01:00
horizontalInput = Input.GetAxis("Horizontal");
2025-02-18 16:06:31 +01:00
// move
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
// flip player when moving left-right
if (horizontalInput > 0.01f)
{
transform.localScale = Vector3.one;
}
else if (horizontalInput < -0.01f)
{
transform.localScale = new Vector3(-1, 1, 1);
}
// wall jump logic
2025-03-03 21:58:44 +01:00
if (wallJumpCooldown > 0.2f)
2025-02-18 16:06:31 +01:00
{
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (onWall() && !isGrounded())
{
body.gravityScale = 0;
body.velocity = Vector2.zero;
}
2025-03-03 21:58:44 +01:00
else
{
body.gravityScale = 2;
}
if (Input.GetKey(KeyCode.W))
{
Jump();
}
}
// jumpwall cd
else
{
wallJumpCooldown += Time.deltaTime;
2025-02-18 16:06:31 +01:00
}
// set animator parameters
anim.SetBool("run", horizontalInput != 0);
2025-03-03 21:58:44 +01:00
anim.SetBool("grounded", isGrounded());
2025-02-18 16:06:31 +01:00
}
private void Jump()
{
2025-03-03 21:58:44 +01:00
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpPower);
anim.SetTrigger("jump");
}
// "climb" on wall
else if (onWall() && !isGrounded())
{
if (horizontalInput == 0)
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 9, 0);
transform.localScale = new Vector3(-Mathf.Sign(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
else
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 3, 6);
}
wallJumpCooldown = 0;
}
2025-02-18 16:06:31 +01:00
}
private void OnCollisionEnter2D(Collision2D collision)
{
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool onWall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
return raycastHit.collider != null;
}
}