Exemple #1
0
    void Update()
    {
        if (driver == null || !driver.IsValid())
        {
            lookController.enabled = false;
            return;
        }

        // Damp any change to our root heading.
        dampedForward      = DampHeading(dampedForward, driver.GetLookDirection().GetForwardHeading());
        transform.rotation = Quaternion.LookRotation(dampedForward, Vector3.up);
        Debug.Assert(Mathf.Abs(transform.forward.y) < 1e-4);

        // Damp throttle to avoid pops. See the comment for ThroughZeroLerp for
        // details.
        prevDampedThrottle = ThroughZeroLerp(prevDampedThrottle, driver.GetMoveThrottle(), transform.forward, LerpForHalfLife(0.1f));
        //TEMP
        prevDampedThrottle = driver.GetMoveThrottle();

        // This code gives the throttle *relative* to the look
        // direction. So if inputX is positive, then we're strafing right by
        // some amount. If we're just moving forward, inputX should be 0, and
        // inputY should be positive. If inputX is 0 and inputY is negative,
        // we're moving straight backward.
        Vector3 relThrottle    = transform.InverseTransformDirection(prevDampedThrottle.normalized) * prevDampedThrottle.magnitude;
        float   speed          = relThrottle.magnitude;
        float   blendMagnitude = 1f;

        if (speed < 4.0f)
        {
            Debug.Assert(speed < IdealRunSpeed);
            // Walking.
            blendMagnitude = 0.5f;
            animator.SetFloat("LocomotionSpeedScale", speed / IdealWalkSpeed);
        }
        else
        {
            // Running
            blendMagnitude = 1f;
            animator.SetFloat("LocomotionSpeedScale", speed / IdealRunSpeed);
        }
        Vector2 blendCoord = new Vector2(relThrottle.x, relThrottle.z).normalized *blendMagnitude;

        animator.SetFloat("InputX", blendCoord.x, inputDampTime, Time.deltaTime);
        animator.SetFloat("InputY", blendCoord.y, inputDampTime, Time.deltaTime);

        // calculate the distance to the ground (which is on it's own layer) and pass that info to the animator so we know how high up we are
        int layer_mask = LayerMask.GetMask("Ground");

        RaycastHit hit = new RaycastHit();

        if (Physics.Raycast(transform.position, -Vector3.up, out hit, layer_mask))
        {
            heightFromGround = hit.distance;
        }

        animator.SetFloat("HeightFromGround", heightFromGround);
        animator.SetBool("IsGrounded", driver.IsGrounded());

        if (!ignoreLook)
        {
            lookController.enabled      = true;
            lookController.lookDirWorld = driver.GetLookDirection();
        }
        else
        {
            lookController.enabled = false;
        }

        DrawOffset(driver.GetLookDirection().GetForwardHeading(), Color.red);
        DrawOffset(driver.GetMoveThrottle(), Color.yellow);
        DrawOffset(prevDampedThrottle, Color.blue);
        DrawOffset(transform.forward, Color.green);
    }
    void Update()
    {
        if (driver == null)
        {
            return;
        }
        animator.SetBool("IsGrounded", driver.IsGrounded());

        Vector3 relativeVelocity = Quaternion.Inverse(Quaternion.LookRotation(transform.forward)) * driver.GetMoveThrottle();

        animator.SetFloat("VelX", relativeVelocity.x / MOVE_SPEED, 0.5f, Time.unscaledDeltaTime * 7.5f);
        animator.SetFloat("VelY", relativeVelocity.z / MOVE_SPEED, 0.5f, Time.unscaledDeltaTime * 7.5f);
    }