Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (target == null)
        {
            //Destroy(gameObject);
            objectPooler.BackToPool(btag, this.gameObject);
            return;
        }

        Vector3 dir;

        dir.x = target.position.x - _transform.position.x;
        dir.y = target.position.y - _transform.position.y;
        dir.z = target.position.z - _transform.position.z;

        float distanceThisFrame = bulletSpeed * Time.deltaTime;
        float dirMagnitude      = dir.x * dir.x +
                                  dir.y * dir.y +
                                  dir.z * dir.z;

        if (dirMagnitude <= distanceThisFrame * distanceThisFrame)
        {
            HitTarget();
            return;
        }

        float   sqrtMagnitude = Mathf.Sqrt(dirMagnitude);
        Vector3 dirNormalized;

        dirNormalized.x = (dir.x / sqrtMagnitude) * distanceThisFrame;
        dirNormalized.y = (dir.y / sqrtMagnitude) * distanceThisFrame;
        dirNormalized.z = (dir.z / sqrtMagnitude) * distanceThisFrame;

        _transform.Translate(dirNormalized, Space.World);
    }