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

How to stop the rotation of a Rigidbody, but for specific collisions ?

Discussion in 'Editor & General Support' started by Deceleris, Oct 28, 2021.

  1. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21
    Hello !

    Is there a way to tell the rigid body "don't rotate with external collision, but rotate when I add to you a force" ? Literally, don't be affected by external interactions, but still be simulated (If the player run into a block of rock, his body's velocity and angula velocity are not affected, but his position is. He can run into obstacles without being disturbs, while being unable to run through them.)
    -------------------------------------------------------------------------------------------------------------------------------------------------

    I'm working on a racing game and I want my player to be able to collide normally with other players and AIs, but I don't want his rotation and speed to be affected by environnement (I want to control the amount of reduction produced by a collision : AI => 100% speed conserved OBSTACLE => 80%)

    It's a bit more complicated than it sound, let me develop :

    1 - I'm using forces to move and rotate the player and AIs (it's necessary, I can't do otherwise)

    2 - Then I can't just freeze the rotation during a collision, (Imagine if the player slide along a rock, if I freeze the rotation, he cannot turn and then he is blocked)

    3 - If I increase the angular drag, I also need to increase the torque force, and there's no relation between the force needed and the drag amount. I mean, if I increase the drag by 2, I need to increase the force sometime by 2 sometime by 10, I don't really know how much exactly, but the result is really uggly (when the drag is really high, if the player release the turn button, the rotation stop almost instantly => If I want to explore this solution, I need to simulate the rotation of the body)

    My solution actually is to freeze during a small amount of time the rotation, and I have also increased the drag unwillingly. But I still not satisfied with that trick.
     
    Astrotype likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Astrotype likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I've worked on a similar problem with lunar lander style games. This is a tough problem, but an important one to solve to make your game awesome.

    If the player commands rotate, I want the controls fairly sharp and tight, stop and start.

    But if they bump against a rock (gently enough that they don't die), I wanted some amount of spin to be imparted.

    I really struggled to find a good heuristic to blend this.

    Ways I tried:

    - made the controls be a direct-rate PD filter - this instantly cancels any unwanted motion (it would fling you off of rocks up into the air)

    - when you bump, momentarily reduce the effectiveness of controls (the delay felt artificial)

    - when you bump, disable control system PD functionality until user taps/releases input (felt even more artificial)

    The best way I have settled on is with a position-driven PD-control system, one that accepts an absolute input ("lean this far please") and converts it into torques.

    This way when you bump, it still transiently upsets the craft, imparting spin, and the torque reaction jets kick in to stabilize you, but there is a moment of upset while they work.

    Here's a quick vid of what I ended up settling on. Pretty pleased with but I'm not sure how directly it applies to race car context. Perhaps one of the above methods would work better?

     
    Astrotype likes this.
  4. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21
    Thanks the god for your awesome answers !

    I would be interested into this solution, It's really what I want (btw thanks for the sharing, always cool to learn new things) but it might me complicated since it's still experimental and on a not released unity version (my project is almost finished and soon ready to be commercialized :p). Kinda sad

    Thanks for your time and the video to demonstrate your solution. I'm not sure to understand what is a direct-rate PD filter, you adding a force only when the key is pressed right ?

    Btw sorry for the lacks of details, it's a boat racing game :D
    That's why it's so hard to handle the collisions, there is no adherence on the ground like a car on the road. When a boat collide with something, it normally ending up by a rotation
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Neat! Yeah, you wanna nail down that feel so it's good for sure.

    I wonder if you might get some interesting results by having the boat controller constantly trying to drive the rigidbody physics with its own "internal" velocity and angular rotation values that it tracks.

    So normally it would be setting a speed based on:

    - internal current speed -> + / - throttle input
    - physics speed -> internal current speed

    Where -> means "moves toward," either with Lerp or with MoveTowards.

    And it would be doing the same for steering, with an internal angular velocity, steering, and the physics angular velocity.

    Then when you hit something, an AI would not affect the current speed in the controller, but it might slow the physics down briefly from the impact, but the controller would quickly try to restore the speed.

    But if you detect impact with an obstacle, it would set the controller's internal speed to 80% and even if the physics hadn't slowed that much, it would slow quickly from the main boat controller and take a moment to regain speed.
     
    Astrotype likes this.
  6. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21
    I finally find a solution !
    It's kinda tricky and I didn't tested it right now but I think it can do the taff, here is it :

    - Simulate the rotation by using torque applied to a separate body child of the boat. This specific body will not be affected by external sources, then I can apply his rotation to the main one
    - The main body, gonna have a classic behavior, but his rotation will not be affected

    Thank you so much @Kurt-Dekker for your investment and the help, and @PraetorBlue by making me discover this topic, you're awesome guys
     
    Astrotype and Kurt-Dekker like this.