Beispiel #1
0
    void Update()
    {
        if (transform.position.y < -50f)
        {
            Destroy(gameObject);
            game.GameOver();
        }
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical   = Input.GetAxisRaw("Vertical");

        velocity = new Vector3(horizontal, 0, vertical).normalized;

        Ray   ray         = viewCamera.ScreenPointToRay(Input.mousePosition);
        Plane groundPlane = new Plane(Vector3.up, hand.position);
        float rayDistance;

        if (groundPlane.Raycast(ray, out rayDistance))
        {
            Vector3 point           = ray.GetPoint(rayDistance);
            Vector3 correctedLookat = new Vector3(point.x, transform.position.y, point.z);
            transform.LookAt(correctedLookat);
            // if magnited < 3f the gun will point towards the ground
            Vector3 distanceBetweenPlayerAndAimPoint = correctedLookat - transform.position;
            if (distanceBetweenPlayerAndAimPoint.magnitude >= 3f)
            {
                hand.LookAt(correctedLookat);
            }
            crosshair.transform.position = point;
            crosshair.CheckIfEnemyDetected(ray);
        }

        if (Input.GetMouseButton(0))
        {
            gun.Shoot();
            gun.fireTriggerReleased = false;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            gun.fireTriggerReleased = true;
        }
        if (Input.GetMouseButtonUp(1))
        {
            isTeleportButtonDown = true;
        }

        if (teleportBar.value <= teleportBar.maxValue)
        {
            teleportBar.value += Time.deltaTime;
        }

        bulletAmount.text = gun.currentBulletsInMagazine + "/" + gun.totalBulletsInMagazine;

        if (gun.isReloading)
        {
            reloadingBarGameObject.SetActive(true);
            reloadingBar.value += Time.deltaTime;
        }
        else
        {
            reloadingBarGameObject.SetActive(false);
            reloadingBar.value = 0;
        }
    }