Esempio n. 1
0
    private void Update()
    {
        Ray ray = new Ray(transform.position, Vector3.forward);

        if (Physics.Raycast(ray, out RaycastHit hitInfo))
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (_stick == null)
                {
                    _stick = Instantiate(_stickTemplate, GetSpawnPsition(hitInfo.point), _stickTemplate.transform.rotation);
                    _rigidbody.velocity = Vector3.zero;
                    if (hitInfo.collider.TryGetComponent(out Block block))
                    {
                        transform.position = _stick.FixationPoint.position;
                        Instantiate(_sparks, hitInfo.point, _sparks.transform.rotation);
                        StartCoroutine(RemoveAfterTime(_stick.gameObject, 0.1f));
                    }
                    if (hitInfo.collider.TryGetComponent(out Segment segment))
                    {
                        _rigidbody.isKinematic = true;
                        transform.position     = _stick.FixationPoint.position;
                        transform.parent       = _stick.FixationPoint;
                    }
                    if (hitInfo.collider.TryGetComponent(out Finish finish))
                    {
                        Debug.Log("Finish!");
                    }
                }
            }

            if (Input.GetMouseButton(0))
            {
                if (_stick != null)
                {
                    _power = (int)(_buttonPressedTimer / _powerAdditingDelay) * _additionPower;
                    _power = Mathf.Clamp(_power, 0, 1);

                    _stick.SetForce((_buttonPressedTimer / _powerAdditingDelay) * _additionPower);

                    _buttonPressedTimer += Time.deltaTime;
                }
            }

            if (Input.GetMouseButtonUp(0))
            {
                Jump();
                _power = 0;
                _buttonPressedTimer = 0;
            }
        }
    }