I’m working on learning some unity basics, so there’s a good chance this’ll seem obvious to fix, but here it is anyways. I’m working on a simple 2D platformer, and have an issue with my movement controller script. In theory, when the player jumps (specifically when a trigger collider leaves the ground), a timer begins ticking down and the jump force calculator takes the value of the timer to diminish the force over time so they can’t jump infinitely high. Unfortunately, when the player leaves the ground the timer stays 1 indefinitely so they can jump as long as they want. What am I missing? My best guess is it has something to do with the OnTriggerEnter/Exit functions, but I don’t know exactly how anything works, especially that (see I’m a beginner excuse above).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement: MonoBehaviour
{
public Rigidbody2D Player;
public bool jump = true;
public float jumpTime = 1f;
float jumpRate = 0f;
public float jumpPow = 12f;
void Update()
{
//calculates the timer on the jump, basically there to slow the player faster
jumpRate = jumpTime * jumpTime * jumpTime * jumpTime * jumpTime;
//subracts the timer of the jump
if (jump = false)
{
jumpTime -= Time.deltaTime;
};
//jump (takes the jumpRate, which is the timer value, and uses it to calculate diminished force over time)
if (Input.GetKey(KeyCode.UpArrow))
{
Player.AddForce(Vector2.up * jumpRate * jumpPow);
};
}
//detects if the player is grounded
void OnTriggerExit2D(Collider2D Platform)
{
jump = false;
}
private void OnTriggerEnter2D(Collider2D Platform)
{
jump = true;
jumpTime = 1f;
}
}