How to change jump height depending on your health

I’m using Javascripts, but my FPSController is the default prefab made with C#, so I don’t really know if I can directly link JS with C#.

That aside, you begin with 100 Health, I’ve made a way to gain and lose health, but the mechanic I want to add is, the less health you have, the higher your jumps are, and when you gain more than 100 (Cap at 200) your jumps are lower.


var JumpModif : int = 0;.

//In the fuction Update

if (Input.GetButtonDown(“Jump”))

{

	Parent_Player.GetComponent.<Rigidbody>().velocity.y = Parent_Player.GetComponent.<Rigidbody>().velocity.y + JumpModif;

}


this piece of code is what I made in order to add more distance in the character’s jumps and the functions responsible of add or substracting JumpModif works fine.

The problem is I either fly infinitely or it simply doesnt work.
Can anyone help?

Thanks!

Velocity of your rigidbody is like a teleport (not exactly), you need add a force to the rigidbody.

Rigidbody MyRigidbody;
    public float jumpModify = 10;
    void Start ()
    {
        MyRigidbody = GetComponent<Rigidbody>();
	}

	void Update ()
    {
		if(Input.GetKeyDown(KeyCode.Space))
        {
            MyRigidbody.AddForce(Vector3.up * jumpModify,ForceMode.Impulse);
        }
    }

There are some diferent Force modes, Impulse is bassed on the mass of the rigidbody, if you add force, and nothing accurs, maybe you need more force, try to find the force that you like.

To adjust your max and min force to your float health, you need save your total health, and get the modify based on your current health:

float totalHealth = 100;
    float currentHealth = 100;
    float force;
    // If the max force necesary to your player is 56 ( vector3.up * 56 )
    float maxForce = 56;

    private void Update()
    {
        //if your currentHealth is 50 (56 / 100 = 0.56) (0.56 * 50 = 28) 28 is half of 56, thats means when your health is 100% your force is 100% etc etc 
        force = (maxForce / totalHealth) * currentHealth;

        if (Input.GetKeyDown(KeyCode.Space))
        {
            MyRigidbody.AddForce(Vector3.up * force, ForceMode.Impulse);
        }
    }