Example #1
0
    /// <summary>
    /// Detects collisions with terrain or moveable objects.
    /// </summary>
    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        float y = hit.normal.y;

        //set motions state according to what we hit...
        if (y > wallLimit)
        {
            //we hit the ground
            hitGround = true;
            moveSound.SetLastGroundObject(hit.collider);

            Vector3 gradient = -Vector3.ProjectOnPlane(hit.normal, Vector3.up);
            float   x        = Vector3.Dot(gradient, horVel.normalized);
            float   atan     = Mathf.Atan2(y, x);
            walkSlope = 90f - (Mathf.Rad2Deg * atan);

            SetPlatform(hit.transform);
            slippery = hit.collider.name.ToLower().Contains("icy");
            if (motionState == MotionState.air && vertVel < 1e-2f)
            {
                Land(hit.collider);
            }
        }
        else if (y > 0f)
        {
            //hit slope we should slide off of
            Vector3 alongVel  = Vector3.ProjectOnPlane(velocity, hit.normal);
            Vector3 normalVel = Vector3.Project(velocity, hit.normal);
            float   r         = 1f - UFUtils.LerpExpFactor(.1f);
            velocity = alongVel + r * normalVel;
        }
        //otherwise we hit a ceiling or wall, cc can handle on its own
    }