Search Unity

Rigidbody won't fall over

Discussion in 'Physics' started by Altissimus, May 11, 2019.

  1. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    I'm applying a collision on a non-Kinematic RigidBody under gravity (interpolate - none; collision detection - discrete).

    The following code turns off the constraints and applies a smack in the face:

    Code (CSharp):
    1.     theRB.constraints = RigidbodyConstraints.None;
    2.     theRB.velocity = new Vector3(Random.Range(GameManager._worldSpeed, -GameManager._worldSpeed), GameManager._worldSpeed, -GameManager._worldSpeed)/2f;
    public Rigidbody theRB; is defined as the Player; the script is on the Player.

    Problem: collision causes movement on the x, y and z axes but zero rotation on any axes. I've tried fiddling with box colliders and model holders and collision speeds and mass, gravity, size/shape of the target box collider, and the angle of my monitor. Nothing makes the damn Player fall over.

    Any ideas what gives?

    Cheers,
    A
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    If you're just adding or setting a velocity, of course it won't rotate due to that. You would have to set a rotational velocity or add a torque.

    But when you say "collisions" you mean the typical this rigidbody has a collider, and there are other colliders in the scene, and this rigidbody hits those? In that case, you may need to look at any other "controller" components which are on the rigidbody. Many character controller scripts force a specific orientation separate from the rigidbody's own constraints, so walking characters don't simply flop over.
     
  3. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    So I take your point about needing to set a rotational velocity, but I thought this would be applied from the physics of the collision. None the less, I've deliberately added some angular velocity (code below) and yet the model still isn't velocitising his angle. Clearly, there's something stopping him from doing so. Given that I've explicitly set his constraints to .None, where would I begin to look for what is getting in the way of my little man spinning his ass off?

    Code (CSharp):
    1.     theRB.constraints = RigidbodyConstraints.None;
    2.     theRB.velocity = new Vector3(Random.Range(GameManager._worldSpeed, -GameManager._worldSpeed), GameManager._worldSpeed, -GameManager._worldSpeed/2f);
    3.     theRB.angularVelocity = new Vector3(Random.Range(GameManager._worldSpeed, -GameManager._worldSpeed),Random.Range(GameManager._worldSpeed, -GameManager._worldSpeed),Random.Range(GameManager._worldSpeed, -GameManager._worldSpeed))/2f;
    4.  
    Edited to add:
    So to be clear there is no actual collision here, because the object colliders are set to "trigger" and the above code is pulled from a OnTriggerEnter(Collider other) call. However, once you take the constraints off, I don't understand why he wouldn't fall down! He's certainly subject to gravity. Moreover, he's still not falling down even with a random angularVelocity added. What could be stopping that?
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    AddForce applies a force directly through the center of mass, which doesnt produce torque.

    You have AddForceAtPosition that takes a force vector and a position (in the rigidbody local space iirc), that will produce a torque with regards to where the force was applied and its direction, if you have rigidbody "rb", an upright capsule, and you AddForceAtPosition with a force of -rb.forward and a position of vector3.up (0,1,0) l, the rigidbody will act as if he was shot in the head and flip back like in the movies. (A real blow to the head doesn't knock you back, maybe just the head a little, in reality you just collapse down)

    Triggers aren't subject to physics afaik, but im on an older version, that might have changed, but i doubt it.


    10/10, great word, would read again.
     
    samochreno likes this.
  5. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    Hi SparrowsNest,

    I now feel an overwhelming pressure to keep up standards of word-usage that I may not be able to attain, but I thank you for my 10 points.

    Appreciate your comments around AddForeceAtPosition which I may well end up using, but I can't get over the dilemma that adding both a random .velocity and a random .angularVelocity should cause the little bugger to fall down.

    Also, I may have misled when I said that there was no collision; what is actually happening is that theRB is stationery and the world is moving (it's an endless runner game), so the objects with the collider are moving, carrying a tranform.position as follows:

    Code (CSharp):
    1.         if(GameManager._canMove)
    2.         {
    3.             transform.position -= new Vector3(0f,0f,GameManager._worldSpeed*Time.deltaTime);
    4.         }
    These objects have Box Coliders with isTrigger = true, but they don't have their own RBs (so does this mean that no physics is applied by them?).

    So...you're a stationery RB that gets hit with a moving object's Box Collider that triggers velocity and angularVelocity, why wouldn't you fall over?
     
    Last edited: May 13, 2019
    SparrowGS likes this.