Greetings All,

Following an online tutorial for creating a health bar and implementing falling damage I tried to tweak the following C# code to make it work with my player combatant in ORK.


using UnityEngine;
using System.Collections;
using ORKFramework;
using ORKFramework.Behaviours;

public class FallDamage : MonoBehaviour


{
//public Transform healthbar;
public float startYPos;
public float endYPos;
public float playerHealth;
public float currentHealth;
public float fallDamage;
public float damageThreshold = 10;
public bool damageMe = false;
public bool firstCall = true;


// Update is called once per frame
void Update ()
{
if (!GameObject.FindObjectOfType<CharacterController>().isGrounded)
{
if (gameObject.transform.position.y > startYPos)
{
firstCall = true;
}
if (firstCall)
{
startYPos = gameObject.transform.position.y;
firstCall = false;
damageMe = true;
}
}

if (GameObject.FindObjectOfType<CharacterController>().isGrounded)
{
endYPos = gameObject.transform.position.y;

if (startYPos - endYPos > damageThreshold)
{
if (damageMe)
{
Combatant combatant = ComponentHelper.GetCombatant(gameObject);
currentHealth = combatant.Status [1].GetValue ();
fallDamage = (startYPos - endYPos - damageThreshold);
playerHealth = (currentHealth - fallDamage);

//healthbar.GetComponent<HealthBar>().Damage(startYPos - endYPos - damageThreshold);
damageMe = false;
firstCall = true;

}
}
}
}

}


Instead of using the health bar from the (Non-ORK) online tutorial I want to use the default combatant HP status value as setup in the ORK tutorial.

There are no errors that pop up in the Console however I am a novice at best in coding and nothing seems to be happening in terms of my player combatant taking damage when falling from any height. I guess I am missing something.

Any assistance would be greatly appreciated.

Many thanks,

DL
  • edited November 2017
    Here you go:


    using UnityEngine;
    using System.Collections;
    using ORKFramework;
    using ORKFramework.Behaviours;

    public class FallDamage : MonoBehaviour
    {
    //public Transform healthbar;
    public float startYPos;
    public float endYPos;
    public float playerHealth;
    public float currentHealth;
    public float fallDamage;
    public float damageThreshold = 10;
    public bool damageMe = false;
    public bool firstCall = true;


    // in-game
    private Combatant combatant;

    void Start()
    {
    this.combatant = ComponentHelper.GetCombatant(gameObject);
    if(this.combatant == null)
    {
    GameObject.Destroy(this);
    }
    }


    // Update is called once per frame
    void Update()
    {
    if(!GameObject.FindObjectOfType<CharacterController>().isGrounded)
    {
    if(gameObject.transform.position.y > startYPos)
    {
    firstCall = true;
    }
    if(firstCall)
    {
    startYPos = gameObject.transform.position.y;
    firstCall = false;
    damageMe = true;
    }
    }

    if(GameObject.FindObjectOfType<CharacterController>().isGrounded)
    {
    endYPos = gameObject.transform.position.y;

    if(startYPos - endYPos > damageThreshold)
    {
    if(damageMe)
    {
    fallDamage = (startYPos - endYPos - damageThreshold);
    this.combatant.Status[1].AddValue((int)-fallDamage, false, true, false, true, true, true);
    damageMe = false;
    firstCall = true;

    }
    }
    }
    }

    }


    It's pretty simple - get the combatant in a Start function (no need to do this every frame) and use the AddValue function of the status value to subtract the fall damage. I've set it up so that it ignores potential barriers :)
    Post edited by gamingislove on
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
Sign In or Register to comment.