コード例 #1
0
ファイル: Player.cs プロジェクト: CarlosGiusti/TileVania
    private void ShootArrow()
    {
        if (myAnimator.GetBool("Shoot"))
        {
            return;
        }
        else if (Time.timeScale == 0)
        {
            return;
        }
        if (Input.GetButtonDown("Fire1"))
        {
            if (FindObjectOfType <GameSesion>().HasArrows())
            {
                // Direccion de la flecha (Posicion del mouse)
                Vector3 shootingDirection = FindObjectOfType <Camera>().ScreenToWorldPoint(Input.mousePosition);
                shootingDirection.z = 0f;

                // Revisar si la direccion es frontal al personaje
                if (Mathf.Sign(transform.localScale.x) > Mathf.Epsilon && transform.position.x > shootingDirection.x)
                {
                    return;
                }
                else if (Mathf.Sign(transform.localScale.x) < Mathf.Epsilon && transform.position.x < shootingDirection.x)
                {
                    return;
                }

                //Activar animacion de disparo
                myAnimator.SetBool("Shoot", true);
                // Consumir Flecha
                FindObjectOfType <GameSesion>().SpendArrow();
                // sonido disparo
                myAudioSource.PlayOneShot(shootClip);
                // Instanciar la flecha
                Arrow arrowShooted = Instantiate(arrow, gun.transform.position, Quaternion.identity);
                // Darle direccion y velocidad
                Vector2 direction = shootingDirection - arrowShooted.transform.position;
                arrowShooted.transform.up = direction;
                arrowShooted.GetComponent <Rigidbody2D>().velocity = direction * arrowSpeed;
                // Si la flecha llego a la posicion objetivo se destruye
                arrowShooted.AddTargetPosition(shootingDirection);
            }
        }
    }