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

can someone help me with this script??

Discussion in '2D' started by Robins132, May 14, 2020.

  1. Robins132

    Robins132

    Joined:
    Apr 4, 2020
    Posts:
    17
    this is a 2d platform game that should go forward automaticly
    if i press mouse button it (the player) should jump

    idk what is wrong



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Security.Cryptography;
    4. using UnityEngine;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour
    7. {
    8.     public Rigidbody2D rb;
    9.     public Transform groundCheck;
    10.     public float groundCheckRadius;
    11.     public LayerMask whatIsGround;
    12.     private bool onGround;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.  
    20.      
    21.     }
    22.  
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         rb.velocity = new Vector2(1, rb.velocity.y);
    28.         onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    29.  
    30.  
    31.         if (Input.GetMouseButtonDown(0) && onGround)
    32.         {
    33.             rb.velocity = new Vector2(rb.velocity.x, 3);
    34.  
    35.         }
    36.     }
    37. }
    38.  
     
  2. nabrown

    nabrown

    Joined:
    Jun 27, 2019
    Posts:
    27
    You'll need to describe what you're actually seeing occurring. Is the character not jumping?
     
  3. UnwiseGIraffe64

    UnwiseGIraffe64

    Joined:
    Jan 7, 2021
    Posts:
    1
    yes it doesn't jump! it likes falls out of the map after it reatches the end of the terrain
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Throw in a Debug.Log in the GetMouseButtonDown part so you can see if it is actually calling or not. If it is calling, then you may need to increase the value of the jump. Create a public float or int called jumpForce or something and replace that 3 with it so you can change it in the inspector while you are testing (during play). My guess is that the mass of the player is too high and the force applied isnt enough.