private void Update()
    {
        Move();

        // 无敌状态下 => 累进无敌时间
        if (m_bIsInvincible)
        {
            m_fInvincibleTimer -= Time.deltaTime;
            // 无敌时间到
            if (m_fInvincibleTimer <= 0)
            {
                m_bIsInvincible = false;
            }
        }

        // 攻击
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Launch();
        }

        // 与NPC对话
        if (Input.GetKeyDown(KeyCode.E))
        {
            RaycastHit2D hit = Physics2D.Raycast(transform.position + new Vector3(0, 0.2f, 0), m_LookDirction, 1.5f, LayerMask.GetMask("NPC"));
            if (hit != null)
            {
                NPCDialog npcDialog = hit.collider.GetComponentInChildren(typeof(NPCDialog), true) as NPCDialog;
                if (npcDialog != null)
                {
                    npcDialog.DisplayDialog();
                }
            }
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical   = Input.GetAxis("Vertical");
        Vector2 move = new Vector2(horizontal, vertical);

        // Use Mathf.Approximately instead of == because the way computers store float numbers means there is a tiny loss in precision.
        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {
            lookDirection.Set(move.x, move.y);
            // you will normalize vectors that store direction because length is not important, only the direction is
            lookDirection.Normalize();
        }

        /*
         * Then you have the three lines that send the data to the Animator,
         * which is the direction you look in and the speed
         * (the length of the move vector).
         */
        animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Look Y", lookDirection.y);
        animator.SetFloat("Speed", move.magnitude);

        if (isInvincible)
        {
            invincibleTimer -= Time.deltaTime;
            Debug.Log(invincibleTimer);
            if (invincibleTimer < 0)
            {
                Debug.Log("i am in if condition");
                isInvincible = false;
            }
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            Launch();
        }

        if (Input.GetKeyDown(KeyCode.X))
        {
            // A layer mask which allows us to test only certain layers. Any layers that are not part of the mask will be ignored during the intersection test. Here, you will select the NPC layer, because that one contains your frog.
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
            if (hit.collider != null)
            {
                NPCDialog character = hit.collider.GetComponent <NPCDialog>();
                if (character != null)
                {
                    character.DisplayDialog();
                }
            }
        }
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        //无敌时间计算
        if (isInvicible)
        {
            invicibleTimer -= Time.deltaTime;
            if (invicibleTimer <= 0)
            {
                isInvicible = false;
            }
        }

        //发射零件修理
        if (shootTimer > 0)
        {
            shootTimer -= Time.deltaTime;
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.Space) || onMobileFire)
            {
                shootTimer = shootTimeGap;
                Launch();
            }
        }

        if (DistanceToNPC() <= 1.3f)
        {
            if (!taskNPC.dialogBox.activeSelf)
            {
                taskNPC.buttonIndicator.SetActive(true);
            }
            else
            {
                taskNPC.buttonIndicator.SetActive(false);
            }
            if (Input.GetKeyDown(KeyCode.F) || onMobileAction)
            {
                taskNPC.DisplayDialog();
            }
        }
        else
        {
            taskNPC.buttonIndicator.SetActive(false);
        }
    }
Ejemplo n.º 4
0
    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical   = Input.GetAxis("Vertical");

        Vector2 move = new Vector2(horizontal, vertical);//用一个二维向量来存储玩家的输入

        //根据玩家的输入来更改朝向,x或y不为0时就说明有移动输入
        //approximately函数是用来做近似值比较的,因为float类型的数值有误差
        if (!Mathf.Approximately(move.x, 0) || !Mathf.Approximately(move.y, 0))
        {
            //赋值loodDirection也可以用 lookDirection = move;来搞定
            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();//标准化
            //为防止多个音效同时播放,需要通过是否正在播放来限定播放音效的唯一性
            if (!walkAudioSource.isPlaying)
            {
                //Play()方法是没办法指定AudioClip的,所以需要用代码赋值AudioClip组件
                walkAudioSource.clip = walkSound;
                walkAudioSource.Play();
            }
        }
        else
        {
            walkAudioSource.Stop();
        }
        //接下来改变动画条件变量
        animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Look Y", lookDirection.y);
        animator.SetFloat("Speed", move.magnitude);//magnitude是取向量的模长

        Vector2 position = transform.position;

        //position.x = position.x + speed * horizontal * Time.deltaTime;
        //position.y = position.y + speed * vertical * Time.deltaTime;
        position = position + speed * move * Time.deltaTime;//horizonta和vertical都放在move向量里了,所以可以直接用

        //transform.position = position;
        rigidbody2d.MovePosition(position);

        //无敌时间
        if (isInvicible)
        {
            invicibleTimer = invicibleTimer - Time.deltaTime;
            if (invicibleTimer <= 0)
            {
                isInvicible = false;
            }
        }
        //按下H发射
        if (Input.GetKeyDown(KeyCode.J))
        {
            Shoot();
        }

        //与NPC对话的方法
        if (Input.GetKeyDown(KeyCode.K))
        {
            //方法的几个变量分别为:发射位置、发射方向、射线距离、需要检测的层级
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f,
                                                 lookDirection, 1.5f, LayerMask.GetMask("NPC"));
            if (hit.collider != null)
            {
                //碰撞器检测成功后,就用它来获取NPC身上的对话框脚本。
                NPCDialog npcDialog = hit.collider.GetComponent <NPCDialog>();
                if (npcDialog != null)
                {
                    npcDialog.DisplayDialog();
                }
            }
        }
    }