Exemple #1
0
        // Update is called once per frame
        void Update()
        {
            if (!running)
            {
                return;
            }
            if (moves[currentState].Compute(Time.deltaTime))
            {
                SetMove(Stand);
            }
            float h = controller.GetHorizontal();
            float v = -controller.GetVertical();

            //clamp to 1
            if (h < -0.5)
            {
                h = -1;
            }
            else if (h > 0.5)
            {
                h = 1;
            }
            if (v < -0.5)
            {
                v = -1;
            }
            else if (v > 0.5)
            {
                v = 1;
            }
            else
            {
                v = 0;
            }
            //Crouch
            if (v <= 0 && jumped)
            {
                jumped = false;
            }
            if (v < 0 && Grounded && Standing())
            {
                SetMove(Crouch);
            }
            else if (v > 0 && Grounded && !jumped)
            {
                Grounded   = false;
                velocity.y = jumpStrength;
                jumped     = true;
            }
            else
            {
                //SetMove(Stand);
            }
            if (Grounded)
            {
                velocity = Vector3.zero;
            }
            //Movement
            if ((currentState == Stand || currentState == Walk) && Grounded)
            {
                velocity += h * speed * Vector3.right;
                if (currentState != Walk && h != 0)
                {
                    SetMove(Walk);
                }
                if (currentState == Walk && h == 0)
                {
                    SetMove(Stand);
                }
            }
            //Direction
            if (opponent)
            {
                if (opponent.transform.position.x < transform.position.x)
                {
                    transform.localScale = new Vector3(-1, 1, 1);
                }
                else
                {
                    transform.localScale = new Vector3(1, 1, 1);
                }
            }
            //Attack
            int curState = currentState;

            if (curState == Walk)
            {
                curState = Stand;
            }
            Node node = moveSet.nodes.Find((Node n) => n.moveId == curState);

            if (node != null)
            {
                foreach (Action a in node.actions)
                {
                    if (controller.GetKeyDown(a.input))
                    {
                        SetMove(a.state);
                    }
                }
            }

            //Apply gravity
            float g = FightManager.gravity;

            velocity           += Vector3.down * g;
            transform.position += velocity * Time.deltaTime;
            if (transform.position.y < FightManager.groundHeight)
            {
                transform.position = new Vector3(transform.position.x, FightManager.groundHeight, 0);
                Grounded           = true;
            }
        }