I’m sorry but there is no sound in the video I need to upgrade the software. I’m currently trying to get the player health bar to decrease when the enemy shoots and hits the player I believe I need to link the player controller with the player health script. How can I link the player health with the player controller script? I would appreciate any feedback
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour
{
[Header("General")]
[Tooltip("In ms^-1")] [SerializeField] float controlSpeed = 24f;
[Tooltip("In m")] [SerializeField] float xRange = 6f;
[Tooltip("In m")] [SerializeField] float yRange = 4f;
[SerializeField] GameObject[] guns;
[Header("Screen-position Based")]
[SerializeField] float positionPitchFactor = -5f;
[SerializeField] float positionYawFactor = 5f;
[Header("Control-throw Based")]
[SerializeField] float controlPitchFactor = -20f;
[SerializeField] float controlRollFactor = -20f;
float xThrow, yThrow;
bool isControlEnabled = true;
private bool landed = true;
private GameController gameController;
private void Start()
{
gameController = FindObjectOfType<GameController>();
}
// Update is called once per frame
void Update()
{
if (isControlEnabled)
{
ProcessTranslation();
ProcessRotation();
ProcessFiring();
}
}
void OnPlayerDeath()
{
isControlEnabled = false;
gameController.GameOver();
}
private void ProcessRotation()
{
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToControlThrow = yThrow * controlPitchFactor;
float pitch = pitchDueToPosition + pitchDueToControlThrow;
float yaw = transform.localPosition.x * positionYawFactor;
float roll = xThrow * controlRollFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
private void ProcessTranslation()
{
xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
yThrow = CrossPlatformInputManager.GetAxis("Vertical");
float xOffset = xThrow * controlSpeed * Time.deltaTime;
float yOffset = yThrow * controlSpeed * Time.deltaTime;
float rawXPos = transform.localPosition.x + xOffset;
float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
float rawYPos = transform.localPosition.y + yOffset;
float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}
void ProcessFiring()
{
if (CrossPlatformInputManager.GetButton("Fire"))
{
SetGunsActive(true);
}
else
{
SetGunsActive(false);
}
}
private void SetGunsActive(bool isActive)
{
foreach (GameObject gun in guns) // care may affect death FX
{
var emissionModule = gun.GetComponent<ParticleSystem>().emission;
emissionModule.enabled = isActive;
}
}
void OnPlayerLanding()
{
isControlEnabled = false;
if (!landed)
{
gameController.GameOver();
}
}
void OnPlayerTakeOff()
{
isControlEnabled = true;
landed = false;
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
[SerializeField] GameObject deathFX;
[SerializeField] Transform parent;
public Image Bar;
public Text Text;
public float max_health = 100f;
public float cur_health = 0f;
//Use this for initialization
void Start()
{
// Initialize the health that is given
cur_health = max_health;
InvokeRepeating("decreaseHealth", 0f, 2f);
}
void Update()
{
if (cur_health <= 0)
{
Destroy(gameObject); //if the player has no health point left, destroy the player.
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<Projectiles>())
{
max_health -= cur_health;
//if the collision object has a homing script, minus player health by damageToPlayer
}
}
void decreaseHealth()
{
//Subtract the health at the following rate
//Check if the health is 0 before we do any damage
if (cur_health < 0)
{
cur_health = 0;
}
//make a new variable and divide the current health my the maximum health
//this is because the fill value goes from 0 to 1
float calc_health = cur_health / max_health; // 70 / 100 = 0.7
SetHealth(calc_health);
//Change the color of the health bar
if (cur_health != 0 && cur_health <= max_health / 1.6 && cur_health > max_health / 2.9) // on the scale of 1000, if health <= 625 and is greater than 345, do the following
{
Bar.color = new Color32(171, 162, 53, 255);
}
else if (cur_health != 0 && cur_health <= max_health / 2.9) // on the scale of 1000, if health <= 625, do the following
{
Bar.color = new Color32(158, 25, 25, 255);
}
}
void SetHealth(float myHealth)
{
Debug.Log(GetInstanceID() + ": SetHealth() called."); // Does the method get called?
Debug.Log(GetInstanceID() + ":SetHealth(), myHealth: " + myHealth); // What value was passed on to it?
//defill the bar based on the current health
Bar.fillAmount = myHealth;
//change the text to display the amount of health
Text.text = cur_health.ToString("f0") + "/100";
}
}