Search Unity

(New to Unity) My jump is weird please help

Discussion in 'Getting Started' started by SelfindulgentNonsense, Oct 27, 2020.

  1. SelfindulgentNonsense

    SelfindulgentNonsense

    Joined:
    Sep 19, 2020
    Posts:
    5
    Hellooo I am working on a game that is supposed to be very floaty. I want there to be a jump. It is not working. I probably forgot something very stupid.
    Can you help me?
    This is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     [Range(1, 10)]
    9.     public Rigidbody2D rb;
    10.     public GameObject DaughterR;
    11.     public GameObject DaughterL;
    12.     public GameObject DaughterU;
    13.     public float rspeed = 0.01f;
    14.     public float lspeed = 0.01f;
    15.     public float Jumpheight = 3f;
    16.     public float Jumpdegreeadjustment = 5f;
    17.  
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         MovementScript();
    23.     }
    24.  
    25.     void MovementScript(){
    26.  
    27.         float hDirection = Input.GetAxis("Horizontal");
    28.         float vDirection = Input.GetAxis("Vertical");
    29.  
    30.         if(hDirection < 0){
    31.             print("hDirection is working in the other direction too boyssssss");
    32.             transform.position = Vector3.MoveTowards(transform.position, DaughterL.transform.position, lspeed);
    33.         }
    34.  
    35.         if(hDirection > 0){
    36.             print("hDirection is working boyssssss");
    37.             transform.position = Vector3.MoveTowards(transform.position, DaughterR.transform.position, rspeed);
    38.         }
    39.  
    40.         if(vDirection > 0){
    41.             print ("vDirection is working too boyssssss");
    42.             GetComponent<Rigidbody2D>().velocity = Vector2.up * Jumpheight;
    43.             transform.eulerAngles = Vector3.forward * Jumpdegreeadjustment;
    44.         }
    45.  
    46.     }
    47.    
    48. }


    That is my whole code.
    Now when I press the jump button, the character stays in the air or floats up depending on how big the value for Jumpheight is. I think the Jumpheight is being applied again and again even though I only want it to be applied once. I dont want a fancy jump, just a very basic and preferably floaty one.
    Please help me it would be much appreciated.
    Have a nice day :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well it is indeed being applied again and again, since you have nothing in there to prevent that. The code just says, if the vertical axis is pushed up, apply the jump velocity.

    You probably need to tell when your character is "grounded" and then apply that jump only when grounded.
     
  3. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    73
    You can create a bool onGround() function doing a very short raycast down and checking if you hit a floor (you can use tags here, or custom ground component scripts if you need more control. One way would be hit.collider.gameObject.CompareTag("Ground") for example, or if you use a custom ground class hit.collider.gameObject.GetComponent<Ground>())

    another way to do this is to use onCollisionEnter(collider) and do the same checks as above to determine whether you hit a 'ground'.

    using that check you can then only do the jump if your character is on the ground.