Advertisement
LeeMace

Touch Bounds Lose Health

Feb 8th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     private float speedMultiplier = 1; //float
  8.     [SerializeField] private bool dead; //true or false
  9.     [SerializeField] private int health; //1,2,3,4,5,6
  10.     [SerializeField] private string playerName = "Billy"; //word, name, phrase, sentence etc
  11.     [SerializeField] private float recoveryCounter;
  12.  
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.         Debug.Log("Hello");
  17.     }
  18.  
  19.     // Update is called once per frame
  20.     void Update()
  21.     {
  22.         //Move me
  23.         transform.position += new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0)*speedMultiplier*Time.deltaTime;
  24.  
  25.         //Increase recoveryCounter 1 per second
  26.         recoveryCounter += Time.deltaTime;
  27.        
  28.         CheckBoundaries();
  29.     }
  30.  
  31.     void Hurt()
  32.     {
  33.         if (recoveryCounter > 2)
  34.         {
  35.             health -= 1;
  36.             recoveryCounter = 0;
  37.             Debug.Log("Hurt: " + health);
  38.         }
  39.     }
  40.  
  41.     void CheckBoundaries()
  42.     {
  43.         //If I'm touching the right of the screen (8.35), stop moving
  44.         if (transform.position.x > 8.35)
  45.         {
  46.             transform.position = new Vector3(8.35f, transform.position.y, transform.position.z);
  47.             Hurt();
  48.         }
  49.  
  50.         //If I'm touching the left of the screen (-8.35), stop moving
  51.         if (transform.position.x < -8.35)
  52.         {
  53.             transform.position = new Vector3(-8.35f, transform.position.y, transform.position.z);
  54.             Hurt();
  55.         }
  56.  
  57.         //If I'm touching the top of the screen (4.5), stop moving
  58.         if (transform.position.y > 4.5)
  59.         {
  60.             transform.position = new Vector3(transform.position.x, 4.5f, transform.position.z);
  61.             Hurt();
  62.         }
  63.  
  64.         //If I'm touching the bottom of the screen (-4.5), stop moving
  65.         if (transform.position.y < -4.5)
  66.         {
  67.             transform.position = new Vector3(transform.position.x, -4.5f, transform.position.z);
  68.             Hurt();
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement