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

Question Linear Drag

Discussion in 'Scripting' started by dmarku26, Nov 10, 2022.

  1. dmarku26

    dmarku26

    Joined:
    Aug 3, 2020
    Posts:
    23
    Hi all,
    I am making the player to slow down the fall while in air if a button is pressed. My believe was that this is done by modifying the linear drag. I was able to write the code that every time the button is pressed while the player is in air the linear drag goes from 0 to a higher number, lets say 10, but the player still falls with the same speed. Any idea if there is a setting in the rigidbody2d that i need to set for the fall to be affected by the linear drag value? Thank you.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Unless you're applying gravity for each object manually, no. You're going to have to apply a constant force to your object upwards.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    I think you have a bug. The above works just fine.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // @kurtdekker - drop this on an empty GameObject, press PLAY, observe.
    6.  
    7. public class FallDrag : MonoBehaviour
    8. {
    9.     IEnumerator Start ()
    10.     {
    11.         while( true)
    12.         {
    13.             var position = new Vector2( Random.Range( -6.0f, 6.0f), 5.0f);
    14.  
    15.             var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    16.             cube.transform.position = position;
    17.             DestroyImmediate( cube.GetComponent<Collider>());
    18.  
    19.             cube.AddComponent<BoxCollider2D>();
    20.  
    21.             var rb2d = cube.AddComponent<Rigidbody2D>();
    22.  
    23.             // get CallAfterDelay class from:
    24.             // https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e
    25.             CallAfterDelay.Create(
    26.                 Random.Range( 0.5f, 1.0f),
    27.                 () => {
    28.                     rb2d.drag = 10;
    29.                 }
    30.             ).transform.SetParent( cube.transform);
    31.  
    32.             CallAfterDelay.Create(
    33.                 10.0f,
    34.                 () => {
    35.                     Destroy( cube);
    36.                 }
    37.             ).transform.SetParent( cube.transform);
    38.  
    39.             yield return new WaitForSeconds( Random.Range( 0.5f, 1.0f));
    40.         }
    41.     }
    42. }
     
    Bunny83 likes this.
  4. dmarku26

    dmarku26

    Joined:
    Aug 3, 2020
    Posts:
    23
    I thought so. I can see the linear drag value increase when i push the button, but the fall is not affected at all. So weird.