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

Once my charachter touches the groun isGorund stays on

Discussion in '2D' started by WaluigiWAAHH, Apr 19, 2020.

  1. WaluigiWAAHH

    WaluigiWAAHH

    Joined:
    Feb 10, 2019
    Posts:
    1
    Im new to making games, csharp and unity. I followed this tutorial I found online and followed the code but I ran into a problem. The guy made a code that you can jump in midair infinatly amount of times. Then he fixed the problem by adding some code that I followed and copied. Now I have this problem where if my character touches the ground once it will think hes touching it all the time. Pls help and thanks. I have 2 codes.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public float jumpForce = 5f;
    9.     public bool isGrounded = false;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         Jump();
    20.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    21.         transform.position += movement * Time.deltaTime * moveSpeed;
    22.     }
    23.  
    24.     void Jump()
    25.     {
    26.         if (Input.GetButtonDown("Jump") && isGrounded == true)
    27.         {
    28.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    29.  
    30.         }
    31.        
    32.     }
    33.  
    34. }
    35.  

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TlaPreveri : MonoBehaviour
    6. {
    7.     GameObject Junak;//lohka da bo problem
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         Junak = gameObject.transform.parent.gameObject;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        
    18.     }
    19.     private void OnCollisionEnter2D(Collision2D collision)
    20.     {
    21.         if(collision.collider.tag == "Ground")
    22.         {
    23.             Junak.GetComponent<Movement>().isGrounded = true;
    24.         }
    25.     }
    26.  
    27.     private void OnCollisionExit(Collision collision)
    28.     {
    29.         if (collision.collider.tag == "Ground")
    30.         {
    31.             Junak.GetComponent<Movement>().isGrounded = false;
    32.         }
    33.     }
    34. }
    35.  
     
  2. MitjaHD

    MitjaHD

    Joined:
    Sep 30, 2018
    Posts:
    64
    You are using OnColissionExit (I guess you have a 2D game with 2D collider). Try using OnCollisionExit2D(Collission2D collision)