private void UpdateFacingDirection()
    {
        // Calculate which way character should be facing
        float   facingWeight            = desiredFacingDirection.magnitude;
        Vector3 combinedFacingDirection = (
            transform.rotation * desiredMovementDirection * (1 - facingWeight)
            + desiredFacingDirection * facingWeight
            );

        combinedFacingDirection = LocomotionUtil.ProjectOntoPlane(combinedFacingDirection, transform.up);
        combinedFacingDirection = alignCorrection * combinedFacingDirection;

        if (combinedFacingDirection.sqrMagnitude > 0.01f)
        {
            Vector3 newForward = LocomotionUtil.ConstantSlerp(
                transform.forward,
                combinedFacingDirection,
                maxRotationSpeed * Time.deltaTime
                );
            newForward = LocomotionUtil.ProjectOntoPlane(newForward, transform.up);
            //Debug.DrawLine(transform.position, transform.position+newForward, Color.yellow);
            Quaternion q = new Quaternion();
            q.SetLookRotation(newForward, transform.up);
            transform.rotation = q;
        }
    }
Ejemplo n.º 2
0
    public void SetDesiredOrientation(Vector3 target)
    {
        Vector3 difference =
            LocomotionUtil.ProjectOntoPlane(
                target - transform.position,
                Vector3.up);

        this.desiredOrientation = Quaternion.LookRotation(difference);
    }
    private void UpdateVelocity()
    {
        CharacterController controller = GetComponent(typeof(CharacterController)) as CharacterController;
        Vector3             velocity   = controller.velocity;

        if (firstframe)
        {
            velocity   = Vector3.zero;
            firstframe = false;
        }
        if (grounded)
        {
            velocity = LocomotionUtil.ProjectOntoPlane(velocity, transform.up);
        }

        // Calculate how fast we should be moving
        Vector3 movement = velocity;

        //bool hasJumped = false;
        jumping = false;
        if (grounded)
        {
            // Apply a force that attempts to reach our target velocity
            Vector3 velocityChange = (desiredVelocity - velocity);
            if (velocityChange.magnitude > maxVelocityChange)
            {
                velocityChange = velocityChange.normalized * maxVelocityChange;
            }
            movement += velocityChange;

            // Jump
            if (canJump && Input.GetButton("Jump"))
            {
                movement += transform.up * Mathf.Sqrt(2 * jumpHeight * gravity);
                //hasJumped = true;
                jumping = true;
            }
        }

        // Apply downwards gravity
        movement += transform.up * -gravity * Time.deltaTime;

        if (jumping)
        {
            movement -= transform.up * -gravity * Time.deltaTime / 2;
        }

        // Apply movement
        CollisionFlags flags = controller.Move(movement * Time.deltaTime);

        grounded = (flags & CollisionFlags.CollidedBelow) != 0;
    }
Ejemplo n.º 4
0
    public override float[] Interpolate(float[] output, bool normalize)
    {
        float[] weights = BasicChecks(output);
        if (weights != null)
        {
            return(weights);
        }
        weights = new float[samples.Length];

        Vector3 outp;

        Vector3[] samp = new Vector3[samples.Length];
        if (output.Length == 2)
        {
            outp = new Vector3(output[0], output[1], 0);
            for (int i = 0; i < samples.Length; i++)
            {
                samp[i] = new Vector3(samples[i][0], samples[i][1], 0);
            }
        }
        else if (output.Length == 3)
        {
            outp = new Vector3(output[0], output[1], output[2]);
            for (int i = 0; i < samples.Length; i++)
            {
                samp[i] = new Vector3(samples[i][0], samples[i][1], samples[i][2]);
            }
        }
        else
        {
            return(null);
        }

        for (int i = 0; i < samples.Length; i++)
        {
            bool  outsideHull = false;
            float value       = 1;
            for (int j = 0; j < samples.Length; j++)
            {
                if (i == j)
                {
                    continue;
                }

                Vector3 sampleI = samp[i];
                Vector3 sampleJ = samp[j];

                float   iAngle, oAngle;
                Vector3 outputProj;
                float   angleMultiplier = 2;
                if (sampleI == Vector3.zero)
                {
                    iAngle          = Vector3.Angle(outp, sampleJ) * Mathf.Deg2Rad;
                    oAngle          = 0;
                    outputProj      = outp;
                    angleMultiplier = 1;
                }
                else if (sampleJ == Vector3.zero)
                {
                    iAngle          = Vector3.Angle(outp, sampleI) * Mathf.Deg2Rad;
                    oAngle          = iAngle;
                    outputProj      = outp;
                    angleMultiplier = 1;
                }
                else
                {
                    iAngle = Vector3.Angle(sampleI, sampleJ) * Mathf.Deg2Rad;
                    if (iAngle > 0)
                    {
                        if (outp == Vector3.zero)
                        {
                            oAngle     = iAngle;
                            outputProj = outp;
                        }
                        else
                        {
                            Vector3 axis = Vector3.Cross(sampleI, sampleJ);
                            outputProj = LocomotionUtil.ProjectOntoPlane(outp, axis);
                            oAngle     = Vector3.Angle(sampleI, outputProj) * Mathf.Deg2Rad;
                            if (iAngle < Mathf.PI * 0.99f)
                            {
                                if (Vector3.Dot(Vector3.Cross(sampleI, outputProj), axis) < 0)
                                {
                                    oAngle *= -1;
                                }
                            }
                        }
                    }
                    else
                    {
                        outputProj = outp;
                        oAngle     = 0;
                    }
                }

                float magI   = sampleI.magnitude;
                float magJ   = sampleJ.magnitude;
                float magO   = outputProj.magnitude;
                float avgMag = (magI + magJ) / 2;
                magI /= avgMag;
                magJ /= avgMag;
                magO /= avgMag;
                Vector3 vecIJ = new Vector3(iAngle * angleMultiplier, magJ - magI, 0);
                Vector3 vecIO = new Vector3(oAngle * angleMultiplier, magO - magI, 0);

                float newValue = 1 - Vector3.Dot(vecIJ, vecIO) / vecIJ.sqrMagnitude;

                if (newValue < 0)
                {
                    outsideHull = true;
                    break;
                }
                value = Mathf.Min(value, newValue);
            }
            if (!outsideHull)
            {
                weights[i] = value;
            }
        }

        // Normalize weights
        if (normalize)
        {
            float summedWeight = 0;
            for (int i = 0; i < samples.Length; i++)
            {
                summedWeight += weights[i];
            }
            if (summedWeight > 0)
            {
                for (int i = 0; i < samples.Length; i++)
                {
                    weights[i] /= summedWeight;
                }
            }
        }

        return(weights);
    }