0
\$\begingroup\$

I have made a script that alters a rigidbody's angular velocity. It works well but has one issue, its speed plateaus past a certain point.

Here is the script:

    void Start(){
    //Get references.
    bat = transform.GetChild (0).gameObject;
    rig = bat.GetComponent<Rigidbody> ();
    //Initialize variables.
    initPos = bat.transform.position;

    initAngle = bat.transform.rotation.eulerAngles.y;
    if (leftSide) {
        maxAngle = bat.transform.rotation.eulerAngles.y - 45;
        rig.centerOfMass = bat.transform.localPosition;
    } else {
        maxAngle = bat.transform.rotation.eulerAngles.y + 45;
        rig.centerOfMass = -bat.transform.localPosition;
    }
}

void Update(){
    if(leftSide){ // True and false does the same but mirrored effect.
        if(Input.GetButton ("Left Bat")){// button pressed?
            if(bat.transform.rotation.eulerAngles.y > maxAngle){//lower than max angle?(since this is the mirrored version, its rot starts at 180 and turns CCW, thus the '>')
                currAngle = -1 * turnSpeed; // then allow turning
            } else {// otherwise
                currAngle = 0f; //prevent further turning
            }
        } else {// button not pressed?
            if(bat.transform.rotation.eulerAngles.y < initAngle){// greater than resting angle?(same reason as above explanation)
                currAngle = 1 * turnSpeed; // then automatically return
            } else {//otherwise
                currAngle = 0f; //stop returning
                bat.transform.rotation = Quaternion.Euler (new Vector3 (bat.transform.rotation.eulerAngles.x, initAngle, bat.transform.rotation.eulerAngles.z));// this statement re-aligns the rotation of the flipper its initial state(instantly) to prevent slight kinks due to inconsistencies.
                bat.transform.position = initPos;//turning the flipper for some reason can deviate its own position(slight but cumulative), this returns the flipper to its initial position. 
            }
        }
    } else { // default version.
        if(Input.GetButton ("Right Bat")){
            if(bat.transform.rotation.eulerAngles.y < maxAngle){
                currAngle = 1 * turnSpeed;
            } else {
                currAngle = 0f;
            }
        } else {
            if(bat.transform.rotation.eulerAngles.y > initAngle){
                currAngle = -1 * turnSpeed;
            } else {
                currAngle = 0f;
                bat.transform.rotation = Quaternion.Euler (new Vector3 (bat.transform.rotation.eulerAngles.x, initAngle, bat.transform.rotation.eulerAngles.z));
                bat.transform.position = initPos;
            }
        }
    }
}

void FixedUpdate(){// rigidbodies should be processed in fixedupdate I think
    rig.angularVelocity = new Vector3( 0, currAngle, 0);
}

The issue is, no matter how high a value I enter for the movement part, it doesn't get faster than a specific speed. I tried hardcoding a value instead of changing the variable but it didn't make a difference. If I enter a small value, the movement is slower though.

\$\endgroup\$
2
  • \$\begingroup\$ How is your damping configured? \$\endgroup\$
    – DMGregory
    Mar 14, 2018 at 18:09
  • \$\begingroup\$ How do you mean? the angle limits? Those should be obvious considering the 'maxAngle' & 'initAngle' variables are visibly used. If you mean how I've smoothed the transitions, I haven't there is no other damping. I found this on Rigidbodies digitalopus.ca/site/… It says the clamping is done intentionally by Unity's physics engine as well as it being changeable in the "Physics" window but I can't seem to find anything related to angular velocity. \$\endgroup\$
    – Aeiou
    Mar 15, 2018 at 14:10

1 Answer 1

0
\$\begingroup\$

I found that Unity has a limit on the max angular velocity for rigidbodies. This can be overridden individually using rigidbodyRef.maxAngularVelocity

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .