Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     public float MoveSpeed = 1;
  9.     public float KillingHeight = -10;
  10.     public uint MaxPlayerLives = 3;
  11.     //UI
  12.     public Text PlayerLivesUI;
  13.     public GameObject GameOverUI;
  14.    
  15.     private Rigidbody rb;
  16.     private int currentLives;
  17.     private bool canMove = true;
  18.    
  19.     private void Start()
  20.     {
  21.         rb = GetComponent<Rigidbody>();
  22.         currentLives = (int)MaxPlayerLives;
  23.         PlayerLivesUI.text = currentLives.ToString();
  24.     }
  25.  
  26.     private void Update()
  27.     {
  28.         Movement();
  29.         CheckForDeath();
  30.     }
  31.    
  32.     private void Movement()
  33.     {
  34.         if (canMove == true)
  35.         {
  36.             float speed = MoveSpeed * Time.deltaTime;
  37.             float axisZ = Input.GetAxis("Vertical");
  38.             float axisX = Input.GetAxis("Horizontal");
  39.        
  40.             rb.AddForce(new Vector3(axisX, 0, axisZ) * speed);
  41.         }
  42.     }
  43.    
  44.     private void CheckForDeath()
  45.     {
  46.         Vector3 playerPosition = transform.position;
  47.         if (playerPosition.y <= KillingHeight)
  48.         {
  49.             transform.position = new Vector3(0, 0.5f, 0);
  50.             rb.velocity = Vector3.zero;
  51.             rb.angularVelocity = Vector3.zero;
  52.             currentLives--;
  53.             PlayerLivesUI.text = currentLives.ToString();
  54.             if (currentLives <= 0)
  55.             {
  56.                 print("Game over");
  57.                 GameOverUI.SetActive(true);
  58.                 canMove = false;
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement