using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Player_Move_Script : MonoBehaviour { public int maxSpeed = 8; public int jumpPower = 1400; public float speed; public bool grounded = true; public bool facingRight = true; public int playerLayer = 8; private float width; private float height; private Animator animator; private AudioManager_Script audioManager; void Start() { width = GetComponent().bounds.size.x; height = GetComponent().bounds.size.y; animator = GetComponent(); audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent(); } void Update() { if (Static_GlobalStats.health <= 0) return; int layerMask = ~(1 << playerLayer); speed = Input.GetAxis("Horizontal"); Rigidbody2D rigidbody = gameObject.GetComponent(); if (Static_GlobalStats.paused) { rigidbody.velocity = Vector2.zero; animator.SetFloat("speed", 0); return; } // Ground Check // Jump On Enemy Check grounded = false; for (float x = gameObject.transform.position.x - width * 0.55f; x <= gameObject.transform.position.x + width * 0.55f; x += 0.1f) { RaycastHit2D groundHit = Physics2D.Raycast(new Vector2(x, gameObject.transform.position.y), Vector2.down, height * 0.8f, layerMask); if (groundHit.collider) { if (groundHit.collider.tag == "Enemy" || groundHit.collider.tag == "PseudoEnemy" || groundHit.collider.tag == "Boss") //Jump on Enemy { if (audioManager) { audioManager.playSfx("kill"); if (Random.Range(0f, 1f) < 0.3f) audioManager.playComment("kill"); } rigidbody.AddForce(Vector2.up * jumpPower); groundHit.collider.GetComponent().color = new Color(1, 0.5f, 0.3f, 1); groundHit.collider.enabled = false; if (groundHit.collider.tag == "Boss") { groundHit.collider.GetComponent().bodyType = RigidbodyType2D.Dynamic; groundHit.collider.GetComponent().AddForce(Vector2.down * 1000); StartCoroutine(WinTheFuckingGame()); } break; } grounded = true; break; } } // Wall Check for (float y = gameObject.transform.position.y - height * 0.4f; y <= gameObject.transform.position.y + height * 0.4f; y += 0.1f) { RaycastHit2D wallHit = Physics2D.Raycast(new Vector2(gameObject.transform.position.x, y), new Vector2(speed, 0), width * 0.55f, layerMask); if (wallHit.collider && wallHit.collider.tag != "Collectable" && wallHit.collider.tag != "Projectile" && wallHit.collider.tag != "Finish") { speed = 0; break; } } // Controls if (Input.GetButtonDown("Jump") && grounded) { jump(); } // Player Direction if ((speed < 0.0f && facingRight) || (speed > 0.0f && !facingRight)) { flipPlayer(); } // Physics rigidbody.velocity = new Vector2(speed * maxSpeed, rigidbody.velocity.y); // Animation animator.SetFloat("speed", Mathf.Abs(speed)); animator.SetBool("grounded", grounded); } // Might as well... void jump() { gameObject.GetComponent().AddForce(Vector2.up * jumpPower); if (audioManager) audioManager.playSfx("jump"); } void flipPlayer() { facingRight = !facingRight; Vector2 localScale = gameObject.transform.localScale; localScale.x *= -1; transform.localScale = localScale; } IEnumerator WinTheFuckingGame() { yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); yield return new WaitForSeconds(0.2f); if (audioManager) audioManager.playSfx("kill"); SceneManager.LoadScene("Menu_Finale"); } }