コード例 #1
0
    public void Align2(float targetOrientation, float timeStep)
    {
        float rotation = targetOrientation - character.transform.rotation.eulerAngles.y;

        rotation = AdditionalVector3Tools.mapAngleToRange(rotation);
        Vector3 charAngles   = character.transform.rotation.eulerAngles;
        float   rotationSize = Mathf.Abs(Mathf.Abs(targetOrientation) - Mathf.Abs(transform.rotation.eulerAngles.y));

        //if we are within the range of satisfaction, stop rotating and return the targets rotation
        if (rotation < slowDownOrientation)
        {
            charAngles.y = targetOrientation;
            character.transform.rotation = Quaternion.Euler(charAngles);
            return;
        }
        float sign                = Mathf.Sign(rotation);
        float goalVelocity        = (sign * maxAngularVelocity) * (rotation * sign) / (sign * slowDownOrientation);
        float angularAcceleration = (goalVelocity - characterAngularVelocity) / timeToTarget;
        float angle               = charAngles.y;

        if (Mathf.Abs(angularAcceleration) < Mathf.Abs(maxAngularAcceleration))
        {
            characterAngularVelocity = characterAngularVelocity + angularAcceleration * timeStep;
        }
        else
        {
            characterAngularVelocity = (sign) * angularAcceleration;
        }
        if (characterAngularVelocity < maxAngularVelocity)
        {
            charAngles.y = angle + characterAngularVelocity * timeStep;
            character.transform.rotation = Quaternion.Euler(charAngles);
            return;
        }
    }
コード例 #2
0
    void Align(float targetOrientation, float timeStep)
    {
        float rotation = targetOrientation - character.transform.rotation.eulerAngles.y;

        rotation = AdditionalVector3Tools.mapAngleToRange(rotation);
        Vector3 charRotation = character.transform.rotation.eulerAngles;

        //if we are within the range of satisfaction, stop rotating and return the targets rotation
        if (Mathf.Abs(Mathf.Abs(targetOrientation) - Mathf.Abs(transform.rotation.eulerAngles.y)) < satisfactionRotation)
        {
            charRotation.y = targetOrientation;
            character.transform.rotation = Quaternion.Euler(charRotation);
            return;
        }


        //rotation sign

        float sign = Mathf.Sign(rotation);
        //now we compute the goal angular velocity

        float goalVelocity = (sign * maxAngularVelocity) * rotation / (sign * slowDownOrientation);

        //current character velocity

        float currCharVelocity = Vector3.Magnitude(character.rigidbody.velocity);

        //compute the angular acceleration

        float angularAcceleration = (goalVelocity - currCharVelocity) / timeToTarget;


        if (Mathf.Abs(angularAcceleration) > Mathf.Abs(maxAngularAcceleration))
        {
            angularAcceleration = maxAngularAcceleration * sign;
        }

        //ensure angular velocity sign is the same as rotation
        if (Mathf.Sign(maxAngularVelocity) != sign)
        {
            angularVelocity *= sign;
        }
        angularVelocity += angularAcceleration * timeStep;

        if (Mathf.Abs(angularVelocity) > maxAngularVelocity)
        {
            angularVelocity = Mathf.Abs(maxAngularVelocity) * sign;
        }



        charRotation.y = character.transform.rotation.eulerAngles.y + angularVelocity * timeStep;
        character.transform.rotation = Quaternion.Euler(charRotation);
    }