Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class PlayerController : MonoBehaviour
- {
- public float MoveSpeed = 1;
- public float KillingHeight = -10;
- public uint MaxPlayerLives = 3;
- //UI
- public Text PlayerLivesUI;
- public GameObject GameOverUI;
- private Rigidbody rb;
- private int currentLives;
- private bool canMove = true;
- private void Start()
- {
- rb = GetComponent<Rigidbody>();
- currentLives = (int)MaxPlayerLives;
- PlayerLivesUI.text = currentLives.ToString();
- }
- private void Update()
- {
- Movement();
- CheckForDeath();
- }
- private void Movement()
- {
- if (canMove == true)
- {
- float speed = MoveSpeed * Time.deltaTime;
- float axisZ = Input.GetAxis("Vertical");
- float axisX = Input.GetAxis("Horizontal");
- rb.AddForce(new Vector3(axisX, 0, axisZ) * speed);
- }
- }
- private void CheckForDeath()
- {
- Vector3 playerPosition = transform.position;
- if (playerPosition.y <= KillingHeight)
- {
- transform.position = new Vector3(0, 0.5f, 0);
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- currentLives--;
- PlayerLivesUI.text = currentLives.ToString();
- if (currentLives <= 0)
- {
- print("Game over");
- GameOverUI.SetActive(true);
- canMove = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement