Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Other character jumps to high in air pls help!

Discussion in 'Scripting' started by thecapitankaty, Nov 12, 2022.

  1. thecapitankaty

    thecapitankaty

    Joined:
    Nov 21, 2020
    Posts:
    11
    pls my character jumps to high in air

    here's the code

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7. using UnityEngine.Advertisements;
    8.  
    9. public class PlayerController : MonoBehaviour
    10. {
    11.     [Header("Ads")]
    12.     public AdsManager ads;
    13.  
    14.     [Header("Side Check")]
    15.     [SerializeField] private Transform sideCheckPosition;
    16.     [SerializeField] private bool sideWall;
    17.     [SerializeField] private float jumpForceOnSide;
    18.     [SerializeField] private float sideCheckRayDistance;
    19.  
    20.     [Header("Player Movement")]
    21.     [SerializeField] private float jumpForce;
    22.     [SerializeField] private float speed;
    23.  
    24.     public Rigidbody2D rb;
    25.  
    26.     [Header("Ground Check")]
    27.     [SerializeField] private bool jumped;
    28.     [SerializeField] private bool isGrounded;
    29.     [SerializeField] private float groundCheckRayDistance;
    30.  
    31.     public Transform groundCheckPosition;
    32.     public LayerMask groundLayer;
    33.  
    34.     // Start is called before the first frame update
    35.     void Start() {
    36.         rb = GetComponent<Rigidbody2D>();
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update() {
    41.         CheckIfGrounded();
    42.         CheckisSide();
    43.         PlayerJump();
    44.     }
    45.  
    46.     void FixedUpdate() {
    47.         PlayerMovement();
    48.  
    49.         if (!isGrounded && jumped) {
    50.             jumped = true;
    51.             rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
    52.         }
    53.     }
    54.  
    55.     void CheckisSide() {
    56.         sideWall = Physics2D.Raycast(sideCheckPosition.position, Vector2.right, sideCheckRayDistance, groundLayer);
    57.  
    58.         if (sideWall) {
    59.             jumped = false;
    60.             rb.AddForce(transform.up * jumpForceOnSide, ForceMode2D.Impulse);
    61.         }
    62.     }
    63.  
    64.     void CheckIfGrounded() {
    65.         isGrounded = Physics2D.Raycast(groundCheckPosition.position, Vector2.down, groundCheckRayDistance, groundLayer);
    66.  
    67.         if (isGrounded) {
    68.             // and we jumped before
    69.             if (jumped) {
    70.                 jumped = false;
    71.             }
    72.  
    73.             PlayerMovement();
    74.         }
    75.     }
    76.  
    77.     void PlayerMovement() {
    78.         rb.AddForce(transform.right * speed, ForceMode2D.Force);
    79.     }
    80.  
    81.     void PlayerJump() {
    82.         if (isGrounded) {
    83.             if (Input.GetMouseButtonDown(0)) {
    84.                 jumped = true;
    85.                 rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
    86.             }
    87.         }
    88.     }
    89.  
    90.     void OnCollisionEnter2D(Collision2D collision) {
    91.         if (collision.gameObject.tag == "Fall") {
    92.             SceneManager.LoadScene("UDed");
    93.             ads.ShowVideoAd();
    94.         }
    95.     }
    96. }
    97.  
    98.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Adjust the
    jumpForce
    quantity down.

    If that's not the problem, here is how you can continue debugging on your own:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. thecapitankaty

    thecapitankaty

    Joined:
    Nov 21, 2020
    Posts:
    11
    making new project and pops and error
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Extra unwanted packages in new projects (collab, testing, rider and other junk):

    https://forum.unity.com/threads/temp-unityengine-testrunner-dll-error.1133938/#post-7287748

    About the fastest way I have found to make a project and avoid all this noise is to create the project, then as soon as you see the files appear, FORCE-STOP (hard-kill) Unity (with the Activity Manager or Task Manager), then go hand-edit the Packages/manifest.json file as outlined in the above post, then reopen Unity.

    Sometimes the package system gets borked from all this unnecessary churn and requires the package cache to be cleared:

    https://stackoverflow.com/questions/53145919/unity3d-package-cache-errors/69779122