Unity general questions

Comments

  • edited
    @Thelangfordian the problem isn't in doing barrel rolls, my current setup already allows for that. The problem I'm trying to solve for is to correct for rolling when my controls are in neutral - so a self-correcting force is what I'm looking for, not for the player to be able to counter the rotation manually. Does that make sense?

    My code that I attached already does do that, but it also has some unforeseen weird vector force in there that makes it accelerate in odd directions when it shouldn't (it should just be purely rotational, no acceleration).

    So how do you self-correct a torque in one axis? I think answering that alone will do.
  • edited
    @Tuism, Ah ok. I see.

    To reduce the torque you would need to add a counter torque (with dampingFactor) until the rigidbody.angularVelocity is zero. To apply this to a single axis you need to convert the angularVelocity to local coordinates, apply dampingFactor, then convert back to global coordinates.

    I tried the following, seems to work. :/

    if(noUserInput) {
                if(rigidbody.angularVelocity != Vector3.zero) {
    
                    Vector3 localAngularVelocity = transform.InverseTransformVector(rigidbody.angularVelocity);
    
                    // Apply damping factor to desired local axis
                    localAngularVelocity.y *= -0.85f;
                    localAngularVelocity.x = 0f;
                    localAngularVelocity.z = 0f;
    
                    Vector3 globalAngularVelocity = transform.TransformVector(localAngularVelocity);
    
                    myRigidbody.AddTorque(globalAngularVelocity);
                }
            }
Sign In or Register to comment.