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

local 2 player jumping problem

Discussion in 'Scripting' started by S3Critsss, Jun 6, 2020.

  1. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    i have a script on 2 players but when they touch eachother aswell as touch the ground and then stop touching each other but continue to touch the ground they cant jump. can someone help me

    this is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Tom_Movement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public float jumpForce = 5f;
    9.  
    10.     public GameObject Tim;
    11.  
    12.     bool isGrounded;
    13.  
    14.     private void Update()
    15.     {
    16.  
    17.         if (Input.GetKey(KeyCode.RightArrow))
    18.         {
    19.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(moveSpeed, 0f));
    20.         }
    21.         if (Input.GetKey(KeyCode.LeftArrow))
    22.         {
    23.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-moveSpeed, 0f));
    24.         }
    25.  
    26.         if (isGrounded)
    27.         {
    28.             if (Input.GetKeyDown(KeyCode.UpArrow))
    29.             {
    30.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    31.             }
    32.         }
    33.     }
    34.  
    35.     private void OnCollisionEnter2D(Collision2D other)
    36.     {
    37.         if (other.collider.tag == "Ground" || other.collider.tag == "Player")
    38.         {
    39.             isGrounded = true;
    40.         }else if (other.collider.tag == "Ground" && other.collider.tag != "Player")
    41.         {
    42.             isGrounded = true;
    43.         }else if (other.collider.tag != "Ground" && other.collider.tag == "Player")
    44.         {
    45.             isGrounded = true;
    46.         }else if (other.collider.tag == "Ground" && other.collider.tag == "Player")
    47.         {
    48.             isGrounded = true;
    49.         }
    50.     }
    51.  
    52.     private void OnCollisionExit2D(Collision2D other)
    53.     {
    54.         isGrounded = false;
    55.     }
    56. }
     
  2. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    maybe your isgrounded is set to false when the 2 players stop touching eachother

    try adding in this part
    Code (CSharp):
    1.    private void OnCollisionExit2D(Collision2D other)
    2.     {
    3.      
    4. if (other.collider.tag == "Ground")
    5.         {
    6.             isGrounded = false;
    7.         }
    8.     }
     
  3. S3Critsss

    S3Critsss

    Joined:
    Jan 6, 2020
    Posts:
    111
    I've tried that already it doesn't work.. i think the problem is if it is touching both the player and the ground then isgrounded becomes true but once you stop touching the other the isgrounded becomes false automatically because it has no if statements. but i've tried addding if statements and it doesn't work.. maybe there is something i forgot