As the title suggests, I am having a few issues with wiring up particular scripts, wiring the PlayerController to the Healthbar script that I’ve learnt to build myself. So far the Healthbar follows the player and shows the %, but it fails to update whenever damage from incoming fire.
PlayerController:
{
public float speed = 15.0f;
public float padding = 1f;
public bool autoPlay = false;
float xmin;
float xmax;
public GameObject Lazer1;
public GameObject Lazer2;
public float LazerSpeed = 10;
public float LazerRepeatRate = 0.2f;
public float Health = 500f;
public AudioClip boom1;
private GameManager _gameManager;
private bool Alive = true;
private void Awake()
{
_gameManager = GameObject.FindObjectOfType<GameManager>();
}
void Start()
{
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 Leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
Vector3 Rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
xmin = Leftmost.x + padding;
xmax = Rightmost.x - padding;
}
void Fire()
{
Vector3 StartPosition1 = transform.position + new Vector3(0.39f, 0.39f, 0);
GameObject Laz1 = Instantiate(Lazer1, StartPosition1, Quaternion.identity) as GameObject;
Laz1.GetComponent<Rigidbody2D>().velocity = new Vector3(0, LazerSpeed, 0);
Vector3 StartPosition2 = transform.position + new Vector3(-0.39f, 0.39f, 0);
GameObject Laz2 = Instantiate(Lazer2, StartPosition2, Quaternion.identity) as GameObject;
Laz2.GetComponent<Rigidbody2D>().velocity = new Vector3(0, LazerSpeed, 0);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
InvokeRepeating("Fire", 0.0001f, LazerRepeatRate);
}
if (Input.GetKeyUp(KeyCode.Space))
{
CancelInvoke("Fire");
}
if (Input.GetKey(KeyCode.LeftArrow))
{
//transform.position += new Vector3(-speed * Time.deltaTime, 0, 0); This is a longer version of the same thing below. The right way is + instead of -.
//Deltatime is used to get speed independent of framerate.
transform.position += Vector3.left * speed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
//Restricts player to Gamespace
float newX = Mathf.Clamp(transform.position.x, xmin, xmax);
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
//Vector3 is for 3D games generally, but can still work in 2D games.
}
public void OnTriggerEnter2D(Collider2D collider)
{
Projectile EnemyFire = collider.gameObject.GetComponent<Projectile>();
if (EnemyFire)
{
Health -= EnemyFire.GetDamage();
EnemyFire.Hit();
if (Health <= 0)
{
AudioSource.PlayClipAtPoint(boom1, transform.position, volume: 10f);
_gameManager.PlayerLosesLife();
Alive = false;
}
}
}
private void Respawn()
{
if (Alive != true) {
SpawnLocation();
}
}
private void SpawnLocation() {
this.transform.position = new Vector3(0f, -4.28f,0f);
}
}
I have been attempting to wire this to the player script. I also hope to wire this to the boss script so that bosses display a bar and the player knows that they are actually doing damage. Afterall, what would Dark Souls be like if you removed the health bar? You’d have no idea if you did any damage at all let alone how close you were with each new technique. I’m certainly not making this game anywhere near as good or complex as those games, but I hope you see my point.
HealthBar:
public class HealthBar : MonoBehaviour {
public Image healthpoints;
public Text ratioHealth;
private float Health = 500;
private float MaxHealth = 500;
private PlayerController playerlife;
private void Awake()
{
playerlife = GameObject.FindObjectOfType<PlayerController>();
}
private void Start()
{
HealthPoints();
}
private void HealthPoints() {
float ratio = Health / MaxHealth;
healthpoints.rectTransform.localScale = new Vector3(ratio, 1, 1);
ratioHealth.text = (ratio*100).ToString("0") + '%';
}
private void TakeDamage(float damage) {
Health -= damage;
if (Health < 0)
{
Health = 0;
Debug.Log("Dead");
}
HealthPoints();
}
This script below I took my time to attempt to write it to the player and respawn the player to that point after he loses a life, however for the moment all that happens it that is respawns where it is the health remains at 0 so single attacks cause another death so I want to find out how to have the hitpoints fill up after the player has been spawned.
Spawning Script:
public class RespawnPoint : MonoBehaviour {
public Transform player;
public Transform respawnPoint;
public void Respawn()
{
player.transform.position = respawnPoint.transform.position;
}
}
I have managed to build some new scripts thanks to help online, but appear to be having difficulty wiring them up. Can anyone suggest how to fix these scripts? After that all I need to do is add visual explosions, dialog boxes and then I’ll start building more units and modified boss units too.
Thank you for any help you can offer 
