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

I cant get a flying moving object to stop.

Discussion in 'Physics' started by Galactic_Muffin, Jan 18, 2016.

  1. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    So Im pretty new to all this and I've been using tutorials for making a duel-stick shooter. Every tutorial I've tried, I've gotten different results than what was in the tutorial. I can't get the object to stop moving. I've tried just moving the object manually with code like:
    Code (CSharp):
    1.     Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical"));
    2.  
    3.     transform.position += moveDirection * moveSpeed * Time.deltaTime;
    and with rigged bodies using force to move the objects:

    Code (CSharp):
    1.         moveHorizontal = Input.GetAxis("Horizontal");
    2.         moveVertical = Input.GetAxis("Vertical");
    3.  
    4.         Debug.Log("Horizontal Axis: " + Input.GetAxis("Horizontal") + "Vertical Axis" + Input.GetAxis("Vertical"));
    5.  
    6.         movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    7.  
    8.         rigidbody.AddForce(movement * moveSpeed * Time.deltaTime);
    I can move the object just fine but it will not slow to a stop when I'm not pressing any keys. it just keeps floating away in the direction it was moving forever.
    I did a lot of research to try and find a solution and everyone was saying that adding Drag and AngularDrag to the ridged body will create "air friction" and make it slow to a stop when no force is applied but I tried bumping these up to ridiculously high values and it still doesn't do anything.

    I can get things to stop when they are on a surface, but in my case, I'm trying to make a ship flying through the air, there is no surface in which to apply friction however i still want it to slow to a stop so that it has at least bearable controls.

    Is there something i'm missing that all these tutorials assume you already have set?
    Any help on this would be appreciated :)
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you try to output the drag values of this rigidbody, just to make sure they are actually set?

    Edit: Did you make sure that transform.position is not set when a rigidbody is used? When you are setting the position manually, you are basically killing the physics and you get unexpected behavior, like broken drag.
     
  3. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    Thanks for replying!
    Yes I set the drag values within the component itself and not in the code. And i also don't set the transform.position at all in the method of using the physics and rigedbody. However i did offset the position of the object in the scene within the editor itself. so though it is not being set in code, is it possible moving the position int he editor could have the same effect? Im setting it to 0 0 0 now.

    *edit*
    That didnt seem to fix anything, however i also did notice that when i debug out input.GetAxis for both Horizontal and Vertical, the values dont change (go back to 0) when I stop pressing a key. the debugged out comment just loops the same value UNLESS i press a different key, then the object will change direction but that vertical or Horizontal axis value will not change after i have lifted my finger from the key. could this be effecting it?
     
    Last edited: Jan 18, 2016
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    You have two options.

    Nasty Hack: rigidbody.velocity = rigidbody.velocity * 0.99f; //will decrease velocity by 1% of total over time. Do this when not adding forces. Its like drag.

    The other option is to calculate local velocity and add negative velocity. If you want to stop on a dime, you would add the exact amount of force in the opposite direction.
     
    Galactic_Muffin likes this.
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    just spotted this in another thread. pretty close to the code you want

    Code (CSharp):
    1. float sidewaysdragmultiplier = 1; //how fast do you want your object to slow down
    2. Vector3 velocity = Transform.InverseTransformDirection(rb.velocity);
    3. rb.AddForce(transform.right*-velocity.x*sidewaysdragmultiplier);
    note: this only affects one dimension of forces
     
    Galactic_Muffin likes this.
  6. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    Welp. I figured out why drag wasnt working. Thanks for all your help guys but this was the issue apparently:

    I had "Gravity" under the Horizontal and Vertical Axes in the InputManager set to 0. setting them to 1 or higher fixed it...

    Correct me if I'm wrong but I set that to 0 because I thought it had to do with my rigged body and as I have something that is flying in the air, I didn't want gravity turned on but it sounds like this "gravity" property in the InputManager is referring to enabling or changing the rate at which the axis values "fall" back to 0 and not actually have anything to do with Unitys physics gravity...

    always something simple and stupid. lol
     
    Last edited: Jan 18, 2016
    Dantus likes this.