コード例 #1
0
    // Update is called once per frame
    void UpdateCall()
    {
        frameNumber += 1;
        //Debug.Log (velocity.magnitude);
        Vector3 startPosition = transform.position;
        startPos = startPosition;
        desiredVelocity = Vector3.zero;
        float gravityEffect = -gravity * frameTime;
        float currentGrip;

        float lastGrip = findGrip (lastSmallestIndex);

        /*if(lastGrip>0f)
        {
            float hor = horStick.returnThrottle();
            if(hor<0f)
                desiredVelocity = new Vector3(Mathf.Cos((angles[lastSmallestIndex]+90f)*Mathf.Deg2Rad),
                                              Mathf.Sin ((angles[lastSmallestIndex]+90f)*Mathf.Deg2Rad),0f)
                    * hor * walkVelocity;
            else if(hor>0f)
                desiredVelocity = new Vector3(Mathf.Cos((angles[lastSmallestIndex]+270f)*Mathf.Deg2Rad),
                                              Mathf.Sin ((angles[lastSmallestIndex]+270f)*Mathf.Deg2Rad),0f)
                    * -hor * walkVelocity;
            //if(desiredVelocity!=Vector3.zero)
            //	Debug.Log(desiredVelocity);
        }
        else if(!grounded2)
        {
            // Get desired velocity for flying
            desiredVelocity = new Vector3(horStick.returnThrottle() * 9999f, verStick.returnThrottle() * 9999f, 0f);
        }*/

        //Debug.Log (desiredVelocity);

        //drawLevel ();
        //drawDistances ();
        Debug.Log ("Start" + frameNumber + " " + grounded2);
        Debug.Log (149 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
        if(grounded2)
        {
            gameObject.GetComponent<Renderer>().material.color = Color.blue;

            /*if(velocity.magnitude <(acceleration.lerpStrength (Vector3.Distance (velocity, desiredVelocity)) * lastGrip * frameTime)
               && horStick.returnThrottle()==0f)
            {
                velocity = Vector3.zero;
                Debug.Log ("Fall->Ground vel set to zero");
            }
            else if(horStick.returnThrottle()==0f)
                Debug.Log ("Velmag " + (velocity.magnitude*1000f));*/

            if(grounded1 &&
               lastSmallestIndex!=-1)
            { // Ground Glue
                //if(velocity==Vector3.zero)
                //	transform.position += new vector3
                transform.position += ((velocity+(directions[lastSmallestIndex]*velocity.magnitude) + new Vector3(0f,gravityEffect*frameTime,0f)))*frameTime;
            }
            else
            {
                transform.position += (velocity + new Vector3(0f,-velocity.magnitude + gravityEffect*frameTime,0f))*frameTime;
            }
            Debug.Log (174 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            // Desired velocity index
            int dVIndex = -1;

            // 2 Check grounded
            checkDistances ();
            int smallestIndex = findSmallestDistance ();
            if(smallestIndex==-1)
                dVIndex = lastSmallestIndex;
            else dVIndex = smallestIndex;
            Debug.Log (184 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            float hor = horStick.returnThrottle();
            bool directionRight = true;
            if(hor>0f)
                directionRight = true;
            else if(hor<0f)
                directionRight = false;
            else if(velocity.x>0f)
                directionRight = true;
            else if(velocity.x<0f)
                directionRight = false;
            // New - Use worst-case (grounded-est) velocity angle
            if(smallestIndex!=-1 && lastSmallestIndex!=-1) // Must have two case
            {
                float si, lsi; // abbreviated, floats for angle comparison

                if(directionRight)
                {
                    si = angles[smallestIndex] - 180f;
                    lsi = angles[lastSmallestIndex] - 180f;
                }
                else
                {
                    si = 360f - angles[smallestIndex];
                    lsi = 360f - angles[lastSmallestIndex];
                }
                if(si>lsi)
                    dVIndex = smallestIndex;
                else dVIndex = lastSmallestIndex;
            }
            Debug.Log (214 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            lastSmallestIndex = smallestIndex;
            currentGrip = findGrip(smallestIndex);
            thegrip = currentGrip;
            transform.position = startPosition;
            Debug.Log (219 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            if(dVIndex==-1)
            {

            }
            else
            {
                if(hor<0f && active)
                    desiredVelocity = new Vector3(Mathf.Cos((angles[dVIndex]+90f)*Mathf.Deg2Rad),
                                                  Mathf.Sin ((angles[dVIndex]+90f)*Mathf.Deg2Rad),0f)
                        * hor * walkVelocity;
                else if(hor>0f && active)
                    desiredVelocity = new Vector3(Mathf.Cos((angles[dVIndex]+270f)*Mathf.Deg2Rad),
                                                  Mathf.Sin ((angles[dVIndex]+270f)*Mathf.Deg2Rad),0f)
                        * -hor * walkVelocity;
            }
            Debug.Log (235 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            if(currentGrip>0f)
            {
                grounded1 = true;
                float deltaAngle = Mathf.Abs(Vector3.Angle(desiredVelocity, velocity));
                if(desiredVelocity!=Vector3.zero && deltaAngle<90f)
                {
                    velocity = Vector3.RotateTowards(velocity, desiredVelocity, (currentGrip*deltaAngle*Mathf.Deg2Rad),0f);
                    Debug.Log (243 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
                }
                else if(desiredVelocity==Vector3.zero)
                {
                    Debug.Log (247 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
                    if(velocity.magnitude<(acceleration.lerpStrength (Vector3.Distance (velocity, desiredVelocity)) * currentGrip * frameTime))
                    {
                        if(velocity!=Vector3.zero)
                            Debug.Log ("GroundContact");
                        velocity = Vector3.zero;
                        moveIntoGroundContact();
                        Debug.Log (254 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
                    }
                    else
                    { // Theres a problem here (-6.9, -7.6, 0.0) (-9.1, -4.8, 0.0)
                        Debug.Log ("--Magn: " + velocity.magnitude);
                        Vector3 expectedVelocity = velocity;
                        if(velocity.x>0f)
                            expectedVelocity = new Vector3(Mathf.Cos((angles[dVIndex]+90f)*Mathf.Deg2Rad),
                                                           Mathf.Sin ((angles[dVIndex]+90f)*Mathf.Deg2Rad),0f) * velocity.magnitude;
                        else if(velocity.x<0f)
                            expectedVelocity = new Vector3(Mathf.Cos((angles[dVIndex]+270f)*Mathf.Deg2Rad),
                                                           Mathf.Sin ((angles[dVIndex]+270f)*Mathf.Deg2Rad),0f) * velocity.magnitude;
                        deltaAngle = Mathf.Abs (Vector3.Angle(expectedVelocity, velocity));
                        Vector3 oldVel = velocity;
                        velocity = Vector3.RotateTowards(velocity, expectedVelocity, (currentGrip*deltaAngle*Mathf.Deg2Rad),0f);
                        if(velocity!=Vector3.zero || oldVel!=Vector3.zero)
                            Debug.Log (oldVel + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
                        Debug.Log (271 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
                    }
                }
            }
            else if(grounded1==false)
            {
                grounded2 = false;
            }
            else
            {
                grounded1 = false;
                if(velocity.y>0f)
                    velocity.y = gravityEffect;
            }
            Debug.Log (285 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            if(spacebarDown>0 && grounded2 && nextJumpIndex<jumpset.Length())
            {
                // Jumping here
                grounded1 = grounded2 = false;
                jumping = true;
                currentJump = getJump();
                currentJumpTime = currentJump.time;
                // Apply jump here, from a function
                if(currentJump.hover)
                    velocity.y = currentJump.power;
                else
                    applyFly(desiredVelocity);
                nextJumpIndex++;
                currentGrip = 0f;
                spacebarDown = 0;
                animSet("jump");
            }
            // Apply translation from velocity
            transform.position += velocity * frameTime;
            Debug.Log (305 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            clip ();
            clipVelocity(startPosition, currentGrip);
            Debug.Log (308 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);
            velocity = Vector3.MoveTowards (velocity, desiredVelocity, acceleration.lerpStrength (Vector3.Distance (velocity, desiredVelocity)) * currentGrip * frameTime);
            Debug.Log (310 + " " + velocity + " " + (transform.position*10f) + " " + grounded2);

            if(velocity.magnitude>0.2f && grounded2)
                animSet("walk");
            else if (grounded2)
                animSet("idle");

            // Special case for ground jitter?
        }
        else // Not grounded // Falling
        {
            if(debugBreakOnFall)
                Debug.Break();
            gameObject.GetComponent<Renderer>().material.color = Color.red;
            if(!(jumping && currentJump.hover))
                velocity.y += gravityEffect;

            desiredVelocity = new Vector3(horStick.returnThrottle() * flightVelocity, verStick.returnThrottle() * flightVelocity, 0f);
            if(!active)
                desiredVelocity = velocity;

            if(jumping)
            {
                if(spacebar>0)
                {
                    if(currentJump.applyOverTime)
                        applyFly(desiredVelocity);

                    currentJumpTime += -frameTime;
                    if(currentJumpTime<0f)
                        jumping = false;
                }
                else jumping = false;
            }
            else
            {
                desiredVelocity = velocity;
                if(horStick.returnThrottle()!=0f)
                    desiredVelocity.x = horStick.returnThrottle() * walkVelocity;
                velocity = Vector3.MoveTowards(velocity, desiredVelocity, airControl.lerpStrength(Vector3.Distance(velocity, desiredVelocity)) * frameTime);
            }

            // 1 Apply X,Y simultaneously
            transform.position += velocity * frameTime;

            // 2 Check distances, see if grounded.
            checkDistances ();
            int smallestIndex = findSmallestDistance ();
            lastSmallestIndex = smallestIndex;
            currentGrip = findGrip(smallestIndex);
            // This might be an issue, clipping before grip is found & applied
            clip(smallestIndex, 1f);

            // 3 If grip > 0f, is grounded again
            if(currentGrip>0f)
            {
                grounded2 = true;
                grounded1 = true;
                nextJumpIndex = 0;
                jumping = false;
                // Apply grip to velocity
            }

            if(!grounded2 && !jumping && spacebarDown>0 && nextJumpIndex<jumpset.Length())
            {
                // Flying here
                currentJump = getJump();
                currentJumpTime = currentJump.time;
                nextJumpIndex++;
                spacebarDown = 0;
                jumping = true;
                animSet("fly");
            }
            clipVelocity(startPosition, currentGrip);
        }
        Debug.Log ("End" + frameNumber);
    }
コード例 #2
0
    // Update is called once per frame
    void UpdateCall()
    {
        count = 0;
        frameNumber += 1;
        //Debug.Log (velocity.magnitude);
        Vector3 startPosition = transform.position;
        startPos = startPosition;
        desiredVelocity = Vector3.zero;
        float gravityEffect = -gravity * frameTime;
        float currentGrip;

        float lastGrip = findGrip (lastSmallestIndex);

        //Debug.Log (desiredVelocity);

        //drawLevel ();
        //drawDistances ();

        if(grounded2)
        {
            gameObject.GetComponent<Renderer>().material.color = Color.blue;

            transform.position += velocity * frameTime;
            //if(velocity.y<0f)
            //	transform.position += new Vector3(0f, -velocity.magnitude * frameTime, 0f);
            checkDistances(true);
            int smallestIndex = findSmallestDistance(true);
            currentGrip = findGrip(smallestIndex);
            if(smallestIndex==-1)
            {
                currentGrip = 0f;
                float groundContactBonus = Mathf.Abs(gravityEffect);
                if(velocity.y>0f)
                    groundContactBonus += velocity.y*frameTime;
                if(moveIntoGroundContact(velocity.magnitude*frameTime + groundContactBonus))
                {
                    checkDistances(true);
                    smallestIndex = findSmallestDistance(true);
                    currentGrip = findGrip(smallestIndex);
                    if(currentGrip>0f)
                    {

                    }
                    else
                    {
                        grounded2 = false;
                        transform.position = startPosition + (velocity * frameTime);
                    }
                }
                else
                {
                    grounded2 = false;
                }
            }

            if(currentGrip==0f && smallestIndex!=-1)
            {
                // Mightve hit a wall, try to find ground?
                Debug.Log ("wall");
                clip (smallestIndex, currentGrip);
                Vector3 storedPosition = transform.position;

                float groundContactBonus = Mathf.Abs(gravityEffect);
                if(velocity.y>0f)
                    groundContactBonus += velocity.y*frameTime;
                if(moveIntoGroundContact(velocity.magnitude*groundBufferFactor + groundContactBonus))
                {
                    checkDistances(true);
                    smallestIndex = findSmallestDistance(true);
                    currentGrip = findGrip(smallestIndex);
                    if(currentGrip>0f)
                    {

                    }
                    else
                    {
                        grounded2 = false;
                        transform.position = storedPosition;
                    }
                }
            }

            if(currentGrip>0f)
            {
                float horThrottle = debugHorThrottle;
                if(horThrottle==0f)
                    desiredVelocity = Vector3.zero;
                else
                    desiredVelocity = returnTangent(smallestIndex) * horThrottle * walkVelocity;
                Vector3 a,b;
                if(desiredVelocity!=Vector3.zero)
                {
                    a = velocity.normalized;
                    b = desiredVelocity.normalized;
                    velocity = Vector3.Dot(a,b) * b * velocity.magnitude;
                }
                velocity = Vector3.MoveTowards (velocity, desiredVelocity, acceleration.lerpStrength (Vector3.Distance (velocity, desiredVelocity)) * currentGrip * frameTime);
            }
            else grounded2 = false;

            clip ();

            if(spacebarDown>0 && grounded2 && nextJumpIndex<jumpset.Length())
            {
                // Jumping here
                grounded1 = grounded2 = false;
                jumping = true;
                currentJump = getJump();
                currentJumpTime = currentJump.time;
                // Apply jump here, from a function
                if(currentJump.hover)
                    velocity.y = currentJump.power;
                else
                    applyFly(desiredVelocity);
                nextJumpIndex++;
                currentGrip = 0f;
                spacebarDown = 0;
                animSet("jump");
            }

            //clipVelocity(startPosition, currentGrip);
            if(velocity.magnitude>0.2f && grounded2)
                animSet("walk");
            else if (grounded2)
                animSet("idle");
            GetComponent<logValues>().addValue(Vector3.Distance(transform.position,startPos)/frameTime);
        }
        else // Not grounded // Falling
        {
            if(debugBreakOnFall)
                Debug.Break();
            gameObject.GetComponent<Renderer>().material.color = Color.red;
            if(!(jumping && currentJump.hover))
                velocity.y += gravityEffect;

            desiredVelocity = new Vector3(horStick.returnThrottle() * flightVelocity, verStick.returnThrottle() * flightVelocity, 0f);
            if(!active)
                desiredVelocity = velocity;

            if(jumping)
            {
                if(spacebar>0)
                {
                    if(currentJump.applyOverTime)
                        applyFly(desiredVelocity);

                    currentJumpTime += -frameTime;
                    if(currentJumpTime<0f)
                        jumping = false;
                }
                else jumping = false;
            }
            else
            {
                desiredVelocity = velocity;
                if(horStick.returnThrottle()!=0f)
                    desiredVelocity.x = horStick.returnThrottle() * walkVelocity;
                //Debug.Log (2 + " " + velocity);
                velocity = Vector3.MoveTowards(velocity, desiredVelocity, airControl.lerpStrength(Vector3.Distance(velocity, desiredVelocity)) * frameTime);
                //Debug.Log (2 + " " + velocity);
            }

            // 1 Apply X,Y simultaneously
            transform.position += velocity * frameTime;

            // 2 Check distances, see if grounded.
            checkDistances ();
            int smallestIndex = findSmallestDistance ();
            lastSmallestIndex = smallestIndex;
            currentGrip = findGrip(smallestIndex);
            // This might be an issue, clipping before grip is found & applied
            clip(smallestIndex, 1f);

            // 3 If grip > 0f, is grounded again
            if(currentGrip>0f)
            {
                grounded2 = true;
                grounded1 = true;
                nextJumpIndex = 0;
                jumping = false;
                // Apply grip to velocity
            }

            if(!grounded2 && !jumping && spacebarDown>0 && nextJumpIndex<jumpset.Length())
            {
                // Flying here
                currentJump = getJump();
                currentJumpTime = currentJump.time;
                nextJumpIndex++;
                spacebarDown = 0;
                jumping = true;
                animSet("fly");
            }
            //Debug.Log (3 + " " + velocity);
            clipVelocity(startPosition, currentGrip);
            //Debug.Log (3 + " " + velocity);
        }
        //Debug.Log ("End" + frameNumber);
    }