Example #1
0
    public void Update()
    {
        if (!_player)
        {
            return;
        }

        // Focus on the Player
        transform.LookAt(_player.transform);

        if (_shootCooldown > 0f)
        {
            // Wait the shootCooldown timer
            _shootCooldown -= Time.deltaTime;
            isShooting      = false;
        }
        else
        {
            if (!CanSeePlayer())
            {
                return;
            }

            // Fire the gun
            _gunFiringScript.Shoot();
            _shootCooldown = shootingRate;
            isShooting     = true;
        }
    }
Example #2
0
    public void Update()
    {
        // look to the cursor
        var cameraRay   = _mainCamera.ScreenPointToRay(Input.mousePosition);
        var groundPlane = new Plane(Vector3.up, Vector3.zero);

        if (groundPlane.Raycast(cameraRay, out var rayLength))
        {
            var pointToLook = cameraRay.GetPoint(rayLength);
            // Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
            transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
        }

        // Use this to enable auto-fire (not recommended): Input.GetButton("Fire1")
        if (Input.GetButtonDown("Fire1"))
        {
            _gunFiringScript.Shoot();
        }
    }