Exemple #1
0
 // Update is called once per frame
 void Update()
 {
     state.Execute();
     if (!state.IsInState(updateHeavyLand))
     {
         camera.transform.localPosition = player.transform.localPosition;
         camera.Translate(new Vector3(0, 0, -50));
     }
 }
 // Update is called once per frame
 void Update()
 {
     state.Execute();
     //Vector3 forwardVec = player.transform.forward;
     //camera.localPosition = new Vector3(forwardVec.x*-10, player.transform.localPosition.y, forwardVec.z * -10);
 }
    // ----------------
    // Main update loop!
    void Update()
    {
        dbgTxt  = "";
        dbgTxt += "velocity: " + controller.velocity + "\n";
        dbgTxt += "flags: " + controller.collisionFlags + "\n";

        // set some globals used by the state machine!
        inputRotate = Input.GetAxis("Horizontal");

        float rawInput = Input.GetAxis("Vertical");

        inputMove = 0f;
        if (fwdDown && rawInput <= 0f)
        {
            fwdDown    = false;
            fwdPress   = 0f;
            fwdRelease = Time.time;
        }
        else if (!fwdDown && rawInput > 0f)
        {
            fwdDown    = true;
            fwdPress   = Time.time;
            fwdRelease = 0f;
        }
        if (backDown && rawInput >= 0f)
        {
            backDown    = false;
            backPress   = 0f;
            backRelease = Time.time;
        }
        else if (!backDown && rawInput < 0f)
        {
            backDown    = true;
            backPress   = Time.time;
            backRelease = 0f;
        }
        if (fwdDown)
        {
            float val = groundAttack.Evaluate(Time.time - fwdPress);
            if (val < 0.001)
            {
                val = 0f;
            }
            inputMove += val;
            dbgTxt    += "fwdDown: " + val + " ";
        }
        else
        {
            float val = groundDecay.Evaluate(Time.time - fwdRelease);
            if (val < 0.001)
            {
                val = 0f;
            }
            inputMove += val;
            dbgTxt    += "fwdUp: " + val + " ";
        }
        if (backDown)
        {
            float val = groundAttack.Evaluate(Time.time - backPress);
            if (val < 0.001)
            {
                val = 0f;
            }
            inputMove -= val;
            dbgTxt    += "backDown: " + val + " ";
        }
        else
        {
            float val = groundDecay.Evaluate(Time.time - backRelease);
            if (val < 0.001)
            {
                val = 0f;
            }
            inputMove -= val;
            dbgTxt    += "backUp: " + val + " ";
        }
        if (inputMove > 1f)
        {
            inputMove = 1f;
        }
        if (inputMove < -1f)
        {
            inputMove = -1f;
        }
        dbgTxt += "inputMove: " + inputMove + "\n";

        horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
        dbgTxt            += "hv: " + horizontalVelocity + "\n";
        horizontalVelocity = transform.InverseTransformDirection(horizontalVelocity);
        dbgTxt            += "xformed hv: " + horizontalVelocity + "\n";
        horizontalSpeed    = horizontalVelocity.z; // want the plus or minus on speed
        verticalSpeed      = controller.velocity.y;
        //float overallSpeed = controller.velocity.magnitude;

        // check for restart
        if (Input.GetKeyDown(KeyCode.R))
        {
            GameEventManager.TriggerGameOver();
            GameEventManager.TriggerGameStart();
            return;
        }

        stateMachine.Execute();

        dbgTxt += "move: " + moveVector + "\n";
        dbgTxt += "xformed mv: " + transform.InverseTransformDirection(moveVector) + "\n";

        // move, and adjust speeds based on collisions.  Need to do this to avoid the horrible sliding motions
        // that the Controller does otherwise
        collisionFlags = controller.Move(moveVector * Time.deltaTime);

        // did our last move result in "grounding"
        isGrounded = ((collisionFlags & CollisionFlags.CollidedBelow) != 0);

        if ((collisionFlags & CollisionFlags.CollidedSides) != 0)
        {
            // keep it moving the same direction but at a VERY small rate (so the collision stays consistently on if the player
            // is pushing in that direction)
            moveVector.x /= 100.0f;
            moveVector.z /= 100.0f;
            moveVector.y /= 2.0f;              // slow down the vertical movement
        }

        if ((collisionFlags & CollisionFlags.CollidedAbove) != 0)
        {
            // start moving down immediately by a little.  Ouch, my head!
            moveVector.y  = -gravity * Time.deltaTime * 2f;
            moveVector.x /= 1.15f;              // slow down sideways movement
            moveVector.z /= 1.15f;              // slow down sideways movement
        }

        // update our collected debugText
        debugText.text = dbgTxt;
    }
    // ----------------
    // Main update loop
    // ----------------
    void Update()
    {
        // The avatar grows is size as it progresses through the level,
        // here temporarily handled by a simple check for position.
        if (avatar.transform.position.z > 100)
        {
            avatar.transform.localPosition = new Vector3(0f, 2f, 0f);
            avatar.transform.localScale    = new Vector3(2f, 2f, 2f);
        }
        if (avatar.transform.position.z > 250)
        {
            avatar.transform.localPosition = new Vector3(0f, 3f, 0f);
            avatar.transform.localScale    = new Vector3(3f, 3f, 3f);
        }
        if (avatar.transform.position.z > 350)
        {
            avatar.transform.localPosition = new Vector3(0f, 4f, 0f);
            avatar.transform.localScale    = new Vector3(4f, 4f, 4f);
        }
        if (avatar.transform.position.z > 400)
        {
            avatar.transform.localPosition = new Vector3(0f, 9f, 0f);
            avatar.transform.localScale    = new Vector3(9f, 9f, 9f);
            if (!speedInc)
            {
                moveSpeed *= 1.25f;
                speedInc   = true;
            }
        }

        // The avatar will appear to be rolling while grounded
        if (isGrounded)
        {
            avatar.transform.Rotate(30f, 0f, 0f);
        }

        // Check to see if the user has decided to make a 90-degree turn
        if (Input.GetKeyDown(KeyCode.LeftArrow) && isGrounded)
        {
            turningLeft = true;
        }
        if (Input.GetKeyDown(KeyCode.RightArrow) && isGrounded)
        {
            turningRight = true;
        }

        // State machine stuff
        inputStrafe = Input.GetAxis("Horizontal");

        // Check to see if reset key has been pressed
        if (Input.GetKeyDown(KeyCode.R))
        {
            avatar.transform.localScale = new Vector3(1f, 1f, 1f);
            speedInc  = false;
            moveSpeed = 5.5f;
//			GameEventManager.TriggerGameOver();
//			GameEventManager.TriggerGameStart();
            return;
        }

        stateMachine.Execute();
        collisionFlags = controller.Move(moveVector * Time.deltaTime);

        // Determine if the avatar is now grounded
        isGrounded = ((collisionFlags & CollisionFlags.CollidedBelow) != 0);

        if ((collisionFlags & CollisionFlags.CollidedAbove) != 0)
        {
            moveVector.y  = -gravity * Time.deltaTime * 2f;
            moveVector.x /= 1.15f;
            moveVector.z /= 1.15f;
        }
    }