Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- private float speedMultiplier = 1; //float
- [SerializeField] private bool dead; //true or false
- [SerializeField] private int health; //1,2,3,4,5,6
- [SerializeField] private string playerName = "Billy"; //word, name, phrase, sentence etc
- [SerializeField] private float recoveryCounter;
- // Start is called before the first frame update
- void Start()
- {
- Debug.Log("Hello");
- }
- // Update is called once per frame
- void Update()
- {
- //Move me
- transform.position += new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0)*speedMultiplier*Time.deltaTime;
- //Increase recoveryCounter 1 per second
- recoveryCounter += Time.deltaTime;
- CheckBoundaries();
- }
- void Hurt()
- {
- if (recoveryCounter > 2)
- {
- health -= 1;
- recoveryCounter = 0;
- Debug.Log("Hurt: " + health);
- }
- }
- void CheckBoundaries()
- {
- //If I'm touching the right of the screen (8.35), stop moving
- if (transform.position.x > 8.35)
- {
- transform.position = new Vector3(8.35f, transform.position.y, transform.position.z);
- Hurt();
- }
- //If I'm touching the left of the screen (-8.35), stop moving
- if (transform.position.x < -8.35)
- {
- transform.position = new Vector3(-8.35f, transform.position.y, transform.position.z);
- Hurt();
- }
- //If I'm touching the top of the screen (4.5), stop moving
- if (transform.position.y > 4.5)
- {
- transform.position = new Vector3(transform.position.x, 4.5f, transform.position.z);
- Hurt();
- }
- //If I'm touching the bottom of the screen (-4.5), stop moving
- if (transform.position.y < -4.5)
- {
- transform.position = new Vector3(transform.position.x, -4.5f, transform.position.z);
- Hurt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement