Quantcast
Viewing latest article 30
Browse Latest Browse All 1488

Please help with the script: If crouch not kill if !crouched kill

I wanna do , that if the Bunny collide with the player (Tag "Player") while the player is crouching that the Bunny cant collide with the player (walk trough the Player) but if the bunny collides while the palyer isn't crouching the player gets destroyed and you get teleportet in a other scene. But it doesnt work. Can someone help me please? This is the Script for the Bunny: using UnityEngine; using UnityEngine.SceneManagement; public class BunnyAI : MonoBehaviour { public float speed = 8f; public float right = 129.05f; public float left = 61.1f; private Vector3 rotation; private void Start() { right = transform.position.x + right; left = transform.position.x - left; rotation = transform.eulerAngles; } private void Update() { transform.Translate(Vector2.left * speed * Time.deltaTime); if (transform.position.x < left) { transform.eulerAngles = rotation - new Vector3(0, 180, 0); } if (transform.position.x > right) { transform.eulerAngles = rotation; } } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.CompareTag("Player")) { PlayerMovement player = collision.gameObject.GetComponent(); if (player != null && !player.isCrouching) { // Spieler wird deaktiviert collision.gameObject.SetActive(false); // Spieler wird in eine neue Szene teleportiert SceneManager.LoadScene("NewSceneName"); } } } } And this from the Player: using UnityEngine; using UnityEngine.UI; public class MobilePlayerController : MonoBehaviour { [SerializeField] private float moveSpeed; [SerializeField] private float jumpForce; [SerializeField] private float sprintSpeed; [SerializeField] private float sprintDuration; [SerializeField] public float sprintCooldown; [SerializeField] private float crouchSpeed; [SerializeField] public float crouchHeight = 0.2487039f; private Animator animator; private Rigidbody2D rb; private bool isGrounded; private bool isJumping; private bool isCrouching; private bool isSprinting; private float sprintTimer; private float sprintCooldownTimer; private const string GROUND_TAG = "Ground"; public Button jumpButton; public Button moveLeftButton; public Button moveRightButton; public Button crouchButton; public Button sprintButton; private bool isMovingLeft; private bool isMovingRight; private void Start() { rb = GetComponent(); animator = GetComponent(); // Zuweisung der Aktionen zu den Buttons jumpButton.onClick.AddListener(Jump); moveLeftButton.onClick.AddListener(ToggleMoveLeft); moveRightButton.onClick.AddListener(ToggleMoveRight); crouchButton.onClick.AddListener(Crouch); sprintButton.onClick.AddListener(StartSprinting); } private void Update() { // Steuere die Walking-Animation basierend auf der Horizontalbewegung if (isMovingLeft) { MoveLeft(); } else if (isMovingRight) { MoveRight(); } else { StopMoving(); } // Steuere die Jump-Animation basierend auf dem Sprungstatus animator.SetBool("IsJumping", isJumping); // Steuere die Sprint-Animation basierend auf dem Sprintstatus animator.SetBool("IsSprinting", isSprinting); // Steuere den Sprint-Cooldown if (sprintCooldownTimer > 0f) { sprintCooldownTimer -= Time.deltaTime; } } private void Jump() { if (isGrounded) { isJumping = true; rb.velocity = new Vector2(rb.velocity.x, jumpForce); } } private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag(GROUND_TAG)) { isGrounded = true; isJumping = false; } } private void OnCollisionExit2D(Collision2D other) { if (other.gameObject.CompareTag(GROUND_TAG)) { isGrounded = false; } } private void ToggleMoveLeft() { if (isMovingLeft) { isMovingLeft = false; StopMoving(); } else { isMovingLeft = true; isMovingRight = false; animator.SetBool("IsWalking", true); transform.rotation = Quaternion.Euler(0f, 180f, 0f); } } private void ToggleMoveRight() { if (isMovingRight) { isMovingRight = false; StopMoving(); } else { isMovingRight = true; isMovingLeft = false; animator.SetBool("IsWalking", true); transform.rotation = Quaternion.Euler(0f, 0f, 0f); } } private void StopMoving() { isMovingLeft = false; isMovingRight = false; animator.SetBool("IsWalking", false); rb.velocity = new Vector2(0f, rb.velocity.y); } private void MoveLeft() { if (!isCrouching) { rb.velocity = new Vector2(-moveSpeed, rb.velocity.y); } } private void MoveRight() { if (!isCrouching) { rb.velocity = new Vector2(moveSpeed, rb.velocity.y); } } private void Crouch() { isCrouching = !isCrouching; if (isCrouching) { transform.localScale = new Vector2(transform.localScale.x, crouchHeight); rb.velocity = new Vector2(rb.velocity.x * crouchSpeed, rb.velocity.y); } else { transform.localScale = new Vector2(transform.localScale.x, 0.4487039f); } } private void StartSprinting() { if (!isSprinting && sprintCooldownTimer <= 0f) { isSprinting = true; sprintTimer = sprintDuration; sprintCooldownTimer = sprintCooldown; } } private void FixedUpdate() { if (isSprinting) { sprintTimer -= Time.fixedDeltaTime; if (sprintTimer <= 0f) { isSprinting = false; rb.velocity = new Vector2(rb.velocity.x / 1.5f, rb.velocity.y); } else { if (isMovingLeft) { rb.velocity = new Vector2(-sprintSpeed, rb.velocity.y); } else if (isMovingRight) { rb.velocity = new Vector2(sprintSpeed, rb.velocity.y); } } } else { if (isMovingLeft) { rb.velocity = new Vector2(-moveSpeed, rb.velocity.y); } else if (isMovingRight) { rb.velocity = new Vector2(moveSpeed, rb.velocity.y); } } } }

Viewing latest article 30
Browse Latest Browse All 1488

Trending Articles