Exemple #1
0
    void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }

        if (npcs.Length > 0)
        {
            NpcScript closest_npc = npcs[0];
            foreach (var i in npcs)
            {
                i.questionmark.SetActive(false);
                if ((closest_npc.transform.position - transform.position).magnitude > (i.transform.position - transform.position).magnitude)
                {
                    closest_npc = i;
                }
            }
            if ((closest_npc.transform.position - transform.position).magnitude < 1)
            {
                closest_npc.questionmark.SetActive(true);
                if (Input.GetKeyDown(KeyCode.Space) && !closest_npc.talking)
                {
                    closest_npc.Talk();
                }
            }
        }

        float hbias = Input.GetAxis("Horizontal");
        float vbias = Input.GetAxis("Vertical");

        if (hbias != 0)
        {
            rb.velocity = new Vector2(hbias * 5, 0);
            //transform.position = new Vector3(transform.position.x + hbias * 0.1f, transform.position.y, transform.position.z);
            CmdSetAnim(Anim2Int(walk_r), hbias < 0);
            dir = hbias < 0 ? 1 : 3;
        }
        else
        {
            rb.velocity = new Vector2(0, vbias * 5);
            //transform.position = new Vector3(transform.position.x, transform.position.y + vbias * 0.1f, transform.position.z);
            if (vbias > 0)
            {
                CmdSetAnim(Anim2Int(walk_u), false);
                dir = 2;
            }
            else if (vbias < 0)
            {
                CmdSetAnim(Anim2Int(walk_d), false);
                dir = 0;
            }
            else
            {
                switch (dir)
                {
                case 0:
                    CmdSetAnim(Anim2Int(idle_d), false);
                    break;

                case 1:
                    CmdSetAnim(Anim2Int(idle_r), true);
                    break;

                case 2:
                    CmdSetAnim(Anim2Int(idle_u), false);
                    break;

                case 3:
                    CmdSetAnim(Anim2Int(idle_r), false);
                    break;
                }
            }
        }
    }