Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jump not working, simple 2D movement controller

Discussion in 'Scripting' started by Bannanaking3, Jun 23, 2022.

  1. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    112
    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).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement: MonoBehaviour
    6. {
    7.     public Rigidbody2D Player;
    8.     public bool jump = true;
    9.     public float jumpTime = 1f;
    10.     float jumpRate = 0f;
    11.     public float jumpPow = 12f;
    12.  
    13.     void Update()
    14.     {
    15.         //calculates the timer on the jump, basically there to slow the player faster
    16.         jumpRate = jumpTime * jumpTime * jumpTime * jumpTime  * jumpTime;
    17.  
    18.         //subracts the timer of the jump
    19.         if (jump = false)
    20.         {
    21.             jumpTime -= Time.deltaTime;
    22.         };
    23.  
    24.         //jump (takes the jumpRate, which is the timer value, and uses it to calculate diminished force over time)
    25.         if (Input.GetKey(KeyCode.UpArrow))
    26.         {
    27.             Player.AddForce(Vector2.up * jumpRate * jumpPow);
    28.         };
    29.     }
    30.     //detects if the player is grounded
    31.  
    32.     void OnTriggerExit2D(Collider2D Platform)
    33.     {
    34.         jump = false;
    35.  
    36.     }
    37.  
    38.     private void OnTriggerEnter2D(Collider2D Platform)
    39.     {
    40.         jump = true;
    41.         jumpTime = 1f;
    42.     }
    43. }
    44.  
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Code (CSharp):
    1. if (jump = false)
    2. {
    3.     jumpTime -= Time.deltaTime;
    4. };
    you need to use == or ! operator

    Code (CSharp):
    1. if (!jump)
    2. {
    3.     jumpTime -= Time.deltaTime;
    4. };
     
  3. Bannanaking3

    Bannanaking3

    Joined:
    Jun 21, 2022
    Posts:
    112
    AAAH! thank you, that works