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);
                }
            }
  • This is a very, very, very basic question, but I don't even know what to google :P

    If I want to make some script/function that I can use in another script by just including it into a script with "using" at the top, how do I do that? Is that a good way of making repeat-use functions?
  • edited
    You need to get an instance of the class that you want to call a function from with GetComponent, so you could have a script attached to your camera and then call Camera.main.GetComponent<MyScript>().MyFunction(). The other way is to declare the function in a class as public static, then you can call it from anywhere with MyClass.MyStaticFunction(), but you can't access instance specific variables or functions from that function. If you just want to use declarations from another namespace, you would use using.

    Thanked by 1Tuism
Sign In or Register to comment.