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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I stop my character from flying around when I click the space key?

Discussion in 'Scripting' started by drakkwyr001, Jul 2, 2019.

  1. drakkwyr001

    drakkwyr001

    Joined:
    Jul 1, 2019
    Posts:
    15
    So I started Unity yesterday and I decided to create a character that moves and jumps, walking and jumping its done, although when I press the space the key I can press it multiple times and the character just flies away. So basically I want to make the jump limited, I tried various methods watch various videos but I cant understand how I can solve it, so the forum seems like the best place to show my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class scr: MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Rigidbody rb;
    9.     public float jumpforce = 1f;
    10.     public bool isGrounded;
    11.  
    12.  
    13.     //Vai obter o rigid body do character
    14.  
    15.     void Start()
    16.     {
    17.  
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     //Movimentaçao do character
    22.     void Update()
    23.     {
    24.         float moveHorizontal = Input.GetAxis("Horizontal");
    25.         float moveVertical = Input.GetAxis("Vertical");
    26.         float moveUp = Input.GetAxis("Jump");
    27.         Vector3 movement = new Vector3(moveHorizontal, moveUp, moveVertical);
    28.  
    29.         rb.AddForce(movement * speed);
    30.  
    31.         if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
    32.         {
    33.             rb.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);
    34.  
    35.         }
    36.         if (isGrounded == false && Input.GetKeyDown(KeyCode.Space))
    37.         {
    38.             rb.AddForce(Vector3.down * jumpforce, ForceMode.Impulse);
    39.         }
    40.     }
    41.    
    42.    
    43. }
     
  2. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    You're applying moveUp force everytime you press the Jump button.
    Code (CSharp):
    1. float moveUp = Input.GetAxis("Jump");
    2.         Vector3 movement = new Vector3(moveHorizontal, moveUp, moveVertical);
     
  3. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    1) Make sure gravity is on on your rigidbody
    2) You are checking if your character isGrounded, but you are not setting it anywhere. You call need to look into RayCasting for that
    3) Remove the second if statement, it will not be needed once you have set and checked the isGrounded boolean.
     
  4. drakkwyr001

    drakkwyr001

    Joined:
    Jul 1, 2019
    Posts:
    15
    Thanks for the help, although as I said Im extremely new and RayCasting is kinda confusing for me at this stage and idk how to properly apply it.
     
  5. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    There are plenty of sollutions.
    The easiest one (no so much code and thinking) is by creating a sphere collider into the feet of the player, and check the spherecollider isTrigger to be on.
    in code just use:
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2.     {
    3.        isGrounded = true;
    4.     }
    5.  
    6. private void OnTriggerExit(Collider other)
    7.     {
    8.        isGrounded = false;
    9.     }
    10.  
    and use this code
    Code (CSharp):
    1. void Update()
    2.     {
    3.         float moveHorizontal = Input.GetAxis("Horizontal");
    4.         float moveVertical = Input.GetAxis("Vertical");
    5.         //float moveUp = Input.GetAxis("Jump");
    6.         Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
    7.         rb.AddForce(movement * speed);
    8.         if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
    9.         {
    10.             rb.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);
    11.         }
    12.         if (isGrounded == false && Input.GetKeyDown(KeyCode.Space))
    13.         {
    14.             rb.AddForce(Vector3.down * jumpforce, ForceMode.Impulse);
    15.         }
    16.     }
    The main problem remains isGrounded.. i show you a simple solution in code but not elegant :)