Example #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (!m_Character.CanCollide())
        {
            return;
        }

        Vector3 m_Move = Vector3.zero;

        //calculate inputs
        float v = 0;
        float h = 0;

        //determine where to turn towards/move towards
        if (walkPoints.Count > 0)
        {
            Vector3 dest = walkPoints.Peek().transform.position;

            //we are looking in transform.forward, change to dest - transform.position
            Vector3 dir = dest - transform.position;
            dir.y = 0;
            Quaternion dirQ = Quaternion.LookRotation(dir, Vector3.up);

            FixRotation(dir, dirQ, ref v, ref h);
        }
        //Finish path staring in direction endingDir
        else if (endingDir != Vector3.zero)
        {
            Quaternion dirQ = Quaternion.LookRotation(endingDir, Vector3.up);
            if (Vector3.Angle(transform.forward, endingDir) > rotError)
            {
                FixRotation(endingDir, dirQ, ref v, ref h);
            }
            else
            {
                endingDir = Vector3.zero;
            }
        }
        //After character has completed route, if idle action exists, perform action
        else if (queuedAction != "")
        {
            m_Character.SetIdleAction(queuedAction, queuedValue);
            queuedAction = "";
        }

        // calculate move direction to pass to character
        if (m_Character.CanMove())
        {
            v *= runSpeed;

            // we use local-relative directions in the case of no main camera
            m_Move = v * transform.forward;
        }
        else
        {
            v = 0;
        }

        //impart gravity continuously
        m_Move += new Vector3(0, -20f, 0);

        // pass all parameters to character controller
        m_Character.cControl.Move(m_Move * Time.fixedDeltaTime);

        //make animation changes
        m_Character.UpdateAnimator(m_Move, v, h * turnSpeed, false);
    }
Example #2
0
    // Fixed update is called in sync with physics
    private void FixedUpdate()
    {
        if (!m_Character.CanCollide() || pauseInterface.Paused)
        {
            return;
        }

        Vector3 m_Move = Vector3.zero;

        // read inputs
        float h      = CrossPlatformInputManager.GetAxis("Horizontal");
        float v      = CrossPlatformInputManager.GetAxis("Vertical");
        bool  crouch = Input.GetKey(KeyCode.C);

        //half speed
        v *= 0.5f;

        // calculate move direction to pass to character
        if (m_Character.CanMove())
        {
            if (v < 0)
            {
                v = 0;
            }

            v *= runSpeed;

            if (m_Cam != null)
            {
                // calculate camera relative direction to move:
                m_Move = v * (transform.forward);// + h * m_Cam.right;
            }
            else
            {
                // we use world-relative directions in the case of no main camera
                m_Move = v * Vector3.forward;// + h * Vector3.right;
            }
        }
        else
        {
            v = 0;
            h = 0;
        }

#if !MOBILE_INPUT
        // walk speed multiplier
        if (runLock)
        {
            m_Move *= 2f;
            v      *= 2f;
        }
#else
        //double speed by default
        m_Move *= 2f;
        v      *= 2f;
#endif


        //impart gravity continuously
        m_Move += new Vector3(0, -40f, 0);

        // pass all parameters to the character controller
        m_Character.cControl.Move(m_Move * Time.fixedDeltaTime);

        //make animation changes
        m_Character.UpdateAnimator(m_Move, v, h * turnSpeed, crouch);

        transform.Rotate(transform.up, h * turnSpeed);

        m_Jump = false;
    }