Example #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (!Input.GetKey(KeyCode.Space))
        {
            if (soundCounter >= 10)
            {
                helpController.SpeedControllerCancelInvoker();
                sparks.Stop();
                smoke.Stop();
                lightning.gameObject.SetActive(false);
                if (weldingMaskBrightness.activeSelf)
                {
                    StartCoroutine(ChangeWeldingMaskBrightness(.6f, false));
                }
                audioSource.mute = true;
                return;
            }
            else
            {
                soundCounter++;
                return;
            }
        }

        if (Physics.Raycast(gameObject.transform.position, gameObject.transform.forward, out hitinfo, wireLength))
        {
            if (objectToSpawn == null)
            {
                return;
            }

            if (!hitinfo.collider.gameObject.CompareTag(WELDING_TAG))
            {
                return;
            }

            if (!weldingBrightnessModus)
            {
                StopAllCoroutines();
                if (mask)
                {
                    StartCoroutine(ChangeWeldingMaskBrightness(.1f, true));
                }
            }
            helpController.GuidingAngleControllerMovement(hitinfo.point.z);

            if ((lastSpawnPosition - hitinfo.point).sqrMagnitude > distanceBetweenSpawns)
            {
                results.distance.Add(wireLength);
                results.speed.Add(hitinfo.point);
                helpController.DistanceController(wireLengthConst - wireLength);
                helpController.ComplementaryAngleController();
                helpController.GuidingAngleController();

                if (wireLength > 0.0f)
                {
                    wireLength  -= wireLoss;
                    soundCounter = 0;
                }

                if (counter >= averageCounter)
                {
                    counter = 0;
                    float x = 0.0f;
                    float y = 0.0f;
                    float z = 0.0f;
                    foreach (Vector3 v in averageSpawnTransform)
                    {
                        x += v.x;
                        y += v.y;
                        z += v.z;
                    }
                    x = x / averageSpawnTransform.Count;
                    y = y / averageSpawnTransform.Count;
                    z = z / averageSpawnTransform.Count;

                    Vector3 average = new Vector3(x, y, z);

                    results.positions.Add(transform.position);
                    results.rotations.Add(transform.rotation);

                    lastSpawnedObject = Instantiate(objectToSpawn, average, Quaternion.Euler(0, 0, 0));
                    if (leftHanded)
                    {
                        helpController.GuidingLineControllerLeft(lastSpawnedObject.transform.localPosition.z);
                    }
                    else if (rightHanded)
                    {
                        helpController.GuidingLineControllerRight(lastSpawnedObject.transform.localPosition.z);
                    }
                    spawnedObjects.Add(lastSpawnedObject);
                    lastSpawnedObject.transform.parent = parentObject.transform;
                    lastSpawnPosition = lastSpawnedObject.transform.position;

                    averageSpawnTransform.Clear();
                }
                else
                {
                    counter++;
                    averageSpawnTransform.Add(hitinfo.point);
                }

                foreach (GameObject spawned in spawnedObjects)
                {
                    float distance = (lastSpawnedObject.transform.position - spawned.transform.position).magnitude;

                    if (distance < glowDistance)
                    {
                        spawned.GetComponent <GlowTime>().AddSomeGlow((distance / glowDistance) * 0.8f);
                        spawned.gameObject.transform.localScale += new Vector3(growFactor, growFactor, growFactor);
                    }
                }
            }
            else
            {
                if (lastSpawnedObject != null)
                {
                    lastSpawnedObject.transform.localScale += (new Vector3(growFactor, growFactor, growFactor) * Time.deltaTime);
                }
            }

            sparks.transform.position = hitinfo.point - gameObject.transform.forward.normalized * 0.1f;
            smoke.transform.position  = hitinfo.point;
            sparks.Play();
            smoke.Play();
            if (electricArc)
            {
                lightning.gameObject.SetActive(true);
            }
            audioSource.mute = false;
            helpController.SpeedControllerInvoker();
        }
        else
        {
            if (wireLength <= (wireLengthConst * 3.0f))
            {
                wireLength += wireFeed;
            }

            if (soundCounter >= 10 || wireLength > (wireLengthConst + 0.004f))
            {
                sparks.Stop();
                smoke.Stop();
                lightning.gameObject.SetActive(false);
                StartCoroutine(ChangeWeldingMaskBrightness(.6f, false));
                audioSource.mute = true;
            }
            else
            {
                soundCounter++;
            }
        }
        wire.transform.localScale = new Vector3(wire.transform.localScale.x, wireLength * 10, wire.transform.localScale.z);
    }