using Unity.VisualScripting.FullSerializer; using UnityEditor.Experimental.GraphView; using UnityEngine; /// /// Playermovement, sowie Animation usw. hier angeschaut: https://www.youtube.com/playlist?list=PLgOEwFbvGm5o8hayFB6skAfa8Z-mw4dPV /// public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private float jumpPower; [SerializeField] private LayerMask groundLayer; [SerializeField] private LayerMask wallLayer; private Rigidbody2D body; private Animator anim; private BoxCollider2D boxCollider; private float wallJumpCooldown; private float horizontalInput; private void Awake() { // grab references for rigidbody & animatior from gameobject body = GetComponent(); anim = GetComponent(); boxCollider = GetComponent(); } private void Update() { horizontalInput = Input.GetAxis("Horizontal"); // 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 if (wallJumpCooldown > 0.2f) { body.velocity = new Vector2(horizontalInput * speed, body.velocity.y); if (onWall() && !isGrounded()) { body.gravityScale = 0; body.velocity = Vector2.zero; } else { body.gravityScale = 2; } if (Input.GetKey(KeyCode.W)) { Jump(); } } // jumpwall cd else { wallJumpCooldown += Time.deltaTime; } // set animator parameters anim.SetBool("run", horizontalInput != 0); anim.SetBool("grounded", isGrounded()); } private void Jump() { 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; } } 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; } }