Search Unity

Question Jump momentun when gravity is inverted acting extremely weird

Discussion in 'Scripting' started by rafoskiki, May 30, 2023.

  1. rafoskiki

    rafoskiki

    Joined:
    Jan 25, 2023
    Posts:
    12
    Hello,i been trying to add gravity modifiers for some time now,and the player does flip around,he can move,and jump perfectly normal during normal gravity (in this case 3f),however when i change it to -3f,things get weird

    The jump does execute but depends on the force its gaining from gravity,and for some weird reason,its able to jump midair even though the groundcheck is detecting nothing (with debug.log)

    I truly have no idea what is even happening anymore,i pray to the unity elders in this one

    Code (CSharp):
    1.  player movement script
    2.  
    3.  
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7.  
    8. public class PM : MonoBehaviour
    9. {
    10.     private float horizontal;
    11.     private float jumpingPower = 24f;
    12.     private bool isFacingRight = true;
    13.  
    14.     public float gravitywanted = 3f;
    15.  
    16.     private bool IsUD = false;
    17.  
    18.     private Vector3 respawnPoint;
    19.     public GameObject FallDetector;
    20.     public Rigidbody2D rb;
    21.     [SerializeField] private Transform groundCheck;
    22.     [SerializeField] private LayerMask groundLayer;
    23.     [SerializeField] private TrailRenderer tr;
    24.     [SerializeField] private float speed = 7f;
    25.  
    26.     private void Start()
    27.     {
    28.         respawnPoint = transform.position;
    29.     }
    30.  
    31.     private void Update()
    32.     {
    33.  
    34.         horizontal = Input.GetAxisRaw("Horizontal");
    35.  
    36.         if (Input.GetButtonDown("Jump") && IsGrounded() && IsUD == false)
    37.         {
    38.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    39.         }
    40.  
    41.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f && IsUD == false)
    42.         {
    43.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    44.         }
    45.  
    46.         //Gravity inverted Jump
    47.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f && IsUD == true)
    48.         {
    49.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * -1f);
    50.         }
    51.          if (Input.GetButtonDown("Jump") && IsGrounded() && IsUD == true)
    52.         {
    53.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    54.         }
    55.  
    56.         Flip();
    57.  
    58.         Debug.Log(IsGrounded() + " DETECTED");
    59.     }
    60.  
    61.     private bool IsGrounded()
    62.     {  
    63.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    64.     }
    65.  
    66.      private void FixedUpdate()
    67.     {
    68.         if(IsUD == false)
    69.         {
    70.              rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    71.         }
    72.  
    73.         if(IsUD == true)
    74.         {
    75.              rb.velocity = new Vector2(-horizontal * -speed, rb.velocity.y);
    76.         }
    77.      
    78.     }
    79.  
    80.     private void Flip()
    81.     {
    82.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    83.         {
    84.             Vector3 localScale = transform.localScale;
    85.             isFacingRight = !isFacingRight;
    86.             localScale.x *= -1f;
    87.             transform.localScale = localScale;
    88.         }
    89.     }
    90.  
    91.  
    92.   private void OnTriggerEnter2D(Collider2D collision)
    93.   {
    94.     if(collision.tag == "FallDetector")
    95.     {
    96.         transform.position = respawnPoint;
    97.        
    98.     }
    99.  
    100.     if(collision.tag == "CheckPoint")
    101.     {
    102.         respawnPoint = transform.position;
    103.     }
    104.  
    105.      if (collision.tag == "Shimmeur") //collider that slows player
    106.      {
    107.         rb.gravityScale = 0.5f;
    108.  
    109.      }
    110.  
    111.         if(collision.tag == "Bouncer")
    112.         {
    113.             GravityChange();
    114.         }
    115.   }
    116.  
    117.      private void GravityChange()
    118.      {
    119.         Vector3 localScale = transform.localScale;
    120.         rb.gravityScale = -3f;
    121.         gameObject.transform.localScale= new Vector3(1,-1,1);
    122.         IsUD = true;
    123.      }
    124.      void OnTriggerExit2D(Collider2D other)
    125.      {
    126.          if (other.gameObject.tag == "Shimmeur")
    127.           rb.gravityScale = 3f;
    128.      }
    129. }