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. Dismiss Notice

Bug Broken Gravity

Discussion in 'Scripting' started by Myst1cS04p, Sep 29, 2023.

  1. Myst1cS04p

    Myst1cS04p

    Joined:
    Oct 10, 2022
    Posts:
    16
    Uhh.. so I was making a third person controller and everything was going fine, i added sliding jumping and moving but then i changed the gravity scale using
    Code (CSharp):
    1. Physics.gravity = -gravityScale * Vector3.up;
    pretty much since then the gravity isnt working. No matter what value i set the gravity scale to be it only applies a gravity of -0.14 which is really weird. I cant seem to find any similar cases. But what I have noticed is that once the player character comes close enough to the ground so that the ground check raycast can hit the floor, gravity returns to normal.
    Here is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using UnityEngine.Windows;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     [Space(20)]
    10.     [Header("MOVEMENT")]
    11.     [Space(20)]
    12.  
    13.     [SerializeField] private float moveSpeed = 10f;
    14.     [SerializeField] private float speedMultiplier = 5f;
    15.     [SerializeField] private float airMultiplier = 0.4f;
    16.     [SerializeField] private float drag = 10f;
    17.     [SerializeField] private float airDrag = 10f;
    18.  
    19.     [Space(20)]
    20.     [Header("INPUTS AND RIGIDBODIES")]
    21.     [Space(20)]
    22.  
    23.     [SerializeField] private Rigidbody rb;
    24.     [SerializeField] private InputActionReference movement, jump, dash;
    25.  
    26.     [Space(20)]
    27.     [Header("JUMPING")]
    28.     [Space(20)]
    29.  
    30.     [SerializeField] private float jumpForce;
    31.     [SerializeField] private LayerMask whatIsGround;
    32.     [SerializeField] private bool isGrounded;
    33.  
    34.     [Space(20)]
    35.     [Header("DASH")]
    36.     [Space(20)]
    37.  
    38.     [SerializeField] private float dashDura = 1f;
    39.     [SerializeField] private float timeSinceDash = 0f;
    40.     [SerializeField] private bool isDashing = false;
    41.     [SerializeField] private float dashForce;
    42.     [SerializeField] private float dashAirMultiplier;
    43.  
    44.     [Space(20)]
    45.     [Header("OTHER")]
    46.     [Space(20)]
    47.  
    48.     [SerializeField] private float gravityScale = 15;
    49.  
    50.  
    51.     private void OnEnable()
    52.     {
    53.         jump.action.performed += Jump;
    54.         dash.action.performed += Dash;
    55.     }
    56.  
    57.     private void OnDisable()
    58.     {
    59.         jump.action.performed -= Jump;
    60.         dash.action.performed -= Dash;
    61.     }
    62.  
    63.     private void Start()
    64.     {
    65.         rb = GetComponent<Rigidbody>();
    66.         rb.freezeRotation = true;
    67.         rb.drag = drag;
    68.     }
    69.  
    70.     private void FixedUpdate()
    71.     {
    72.         if (isDashing)
    73.         {
    74.             KeepDashing();
    75.             timeSinceDash += Time.deltaTime;
    76.             if(timeSinceDash >= dashDura)
    77.             {
    78.                 isDashing = false;
    79.                 timeSinceDash = 0;
    80.             }
    81.         }
    82.  
    83.         DragControl();
    84.         GroundCheck();
    85.         Move();
    86.     }
    87.  
    88.     private void GroundCheck()
    89.     {
    90.         if(Physics.Raycast(transform.position, Vector3.down, (transform.localScale.y / 2) + 0.6f, whatIsGround))
    91.         {
    92.             isGrounded = true;
    93.         }
    94.         else
    95.         {
    96.             isGrounded = false;
    97.         }
    98.     }
    99.  
    100.     private void Jump(InputAction.CallbackContext context)
    101.     {
    102.         if (isGrounded)
    103.         {
    104.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    105.             isGrounded = false;
    106.         }
    107.     }
    108.  
    109.     private void DragControl()
    110.     {
    111.         if (isGrounded)
    112.         {
    113.             rb.drag = drag;
    114.         }
    115.         else if(!isGrounded)
    116.         {
    117.             rb.drag = airDrag;
    118.         }
    119.     }
    120.  
    121.     private void Move()
    122.     {
    123.         Vector2 input = movement.action.ReadValue<Vector2>();
    124.  
    125.         Vector3 moveDir = transform.forward * input.y + transform.right * input.x;
    126.  
    127.         if (isGrounded)
    128.         {
    129.             rb.AddForce(moveSpeed * speedMultiplier * moveDir.normalized, ForceMode.Acceleration);
    130.         }
    131.         else
    132.         {
    133.             rb.velocity = moveDir.normalized* moveSpeed;
    134.             rb.AddForce(airMultiplier* moveSpeed * speedMultiplier * moveDir.normalized, ForceMode.Acceleration);
    135.  
    136.         }
    137.     }
    138.  
    139.     private void Dash(InputAction.CallbackContext context)
    140.     {
    141.         if (!isDashing)
    142.         {
    143.             if (isGrounded)
    144.                 rb.AddForce(transform.forward * dashForce, ForceMode.Impulse);
    145.             else
    146.                 rb.AddForce(transform.forward * dashForce * dashAirMultiplier, ForceMode.Impulse);
    147.             isDashing = true;
    148.         }
    149.     }
    150.     private void KeepDashing()
    151.     {
    152.         if (isGrounded)
    153.             rb.AddForce(transform.forward * (dashForce / dashDura), ForceMode.Impulse);
    154.         else
    155.             rb.AddForce(transform.forward * (dashForce / dashDura) * dashAirMultiplier, ForceMode.Impulse);
    156.     }
    157. }
    158.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    So where does
    gravityScale
    get used in your code? The serialised field isn't used anywhere.

    Also I wouldn't change the global gravity scale, as that will affect gravity of everything.
     
  3. Myst1cS04p

    Myst1cS04p

    Joined:
    Oct 10, 2022
    Posts:
    16
    I removed that line of code while debugging it didnt solve the problem but i was using Physics.gravity in fixedupdate
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Well probably because you're setting the velocity every fixed update in line 133. You're nullifying pretty much all vertical velocity by setting the velocity before applying a force.
     
    Myst1cS04p likes this.
  5. Myst1cS04p

    Myst1cS04p

    Joined:
    Oct 10, 2022
    Posts:
    16
    Ah im dumb i forgot to comment that out after trying a different movement method for more snappy controls
    tysm
     
  6. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,448
  7. Myst1cS04p

    Myst1cS04p

    Joined:
    Oct 10, 2022
    Posts:
    16
  8. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,448
    So I gathered but creating several identical threads is considered spam here.
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Just report the post and let the mods address this if they so chose to. No need to harass people about this stuff.
     
  10. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,448
    It's not for me to do that; the poster needs to contact the moderators (by reporting any of the comments) and ask them to merge the threads.
    And informing people is not harassment.