public override Status Update()
    {
        Vector3 dir = (controller._TargetPos
                       - controller.transform.position).normalized;

        Ray        ray = new Ray(controller.transform.position, dir);
        RaycastHit hit;

        bool isInAngleView = Vector3.Angle(
            controller.transform.forward, dir) <= controller._MaxAngleView;

        GLDebug.DrawRay(ray.origin, ray.direction * controller._MaxDstView,
                        isInAngleView ? Color.green : Color.red);

        if (isInAngleView && Physics.Raycast(ray, out hit, controller._MaxDstView) &&
            hit.transform.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            //Debug.Log("Target is visible!");

            return(Status.Success);
        }
        else
        {
            //Debug.Log("Target isn't visible!");

            return(Status.Failure);
        }
    }
Exemple #2
0
    void DrawNormal(Vector3 a, Vector3 b, Vector3 origin)
    {
        Vector3 tan      = (b - a).normalized;
        Vector3 binormal = Vector3.Cross(tan, Vector3.up);
        Vector3 normal   = Vector3.Cross(binormal, tan);

        GLDebug.DrawRay(origin, normal, Color.cyan, 0, true);
    }
Exemple #3
0
    // ------------------------------------------
    private void LookAtMousePos()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 1000f, LayerMask.GetMask("Ground")))
        {
            Vector3 dir = (new Vector3(hit.point.x,
                                       spawnerT.position.y, hit.point.z) - spawnerT.position);
            GLDebug.DrawRay(spawnerT.position, dir.magnitude * spawnerT.up);

            transform.LookAt(new Vector3(hit.point.x, transform.position.y, hit.point.z));
        }
    }
Exemple #4
0
    void Update()
    {
        if (Input.GetKeyDown("r"))
        {
            Application.LoadLevel(Application.loadedLevel);
        }

        // MOUSE : Power Adjust
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            dist += Input.GetAxis("Mouse ScrollWheel");
            //if (dist>minDist) dist=minDist;
            dist = Mathf.Clamp(dist, maxDist, minDist);
            //Debug.Log("dist:"+dist);
            slider.value = -maxDist - minDist - (dist + -maxDist - minDist);
            //slider.image.color = Color.Lerp(Color.green,Color.red,slider.normalizedValue);
            //Debug.Log(slider.normalizedValue);
        }

        // MOUSE: Get mouse position
        Vector3 mPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));

        // rotate stick around ball
        if (follow)
        {
            // TODO: add force distance here?
            Vector3 temp = (stick.position - ball.position).normalized;
            stick.rotation       = Quaternion.LookRotation(Vector3.forward, mPos - ball.position);
            stick.position       = ball.position;
            stick.localPosition += stick.up * dist;

            // raycast from ball to forward
            hit = Physics2D.CircleCast(ball.position, ballRadius, stick.up, 99, layerMaskBallsAndWalls);

            // we hit other balls
            if (hit.collider != null)
            {
                // from stick/ball to target
                GLDebug.DrawLine(stick.position, hit.centroid, Color.white, 0, false);

                // circle preview gizmo
                GLDebug.DrawCircle(hit.centroid, ballRadius, Color.yellow, 0, false);

                // get reflection direction for whiteball
                Vector3 reflectDir = Vector3.Reflect((new Vector3(hit.centroid.x, hit.centroid.y, 0) - stick.position).normalized, hit.normal);

                GLDebug.DrawRay(hit.centroid, reflectDir, Color.yellow, 0, false);

                // target ball estimated direction
                if (hit.collider.CompareTag("Ball"))
                {
                    Vector3 targetDir = (hit.transform.position - new Vector3(hit.centroid.x, hit.centroid.y, 0)).normalized * 10;
                    GLDebug.DrawRay(hit.centroid, targetDir, Color.red, 0, false);
                }
            }
            else
            { // no hit on balls
              // then raycast to walls instead
                hit = Physics2D.CircleCast(stick.position, ballRadius, stick.up, 99, layerMaskWalls);
                if (hit.collider != null)
                {
                    GLDebug.DrawLine(stick.position, hit.centroid, Color.red, 0, false);
                    Vector3 reflectDir2 = Vector3.Reflect((new Vector3(hit.centroid.x, hit.centroid.y, 0) - stick.position).normalized, hit.normal);
                    GLDebug.DrawRay(hit.centroid, reflectDir2, Color.red, 0, false);
                }
                else
                {
                }
            }
        }

        // MOUSE: Shoot
        if (follow && Input.GetMouseButtonUp(0)) // && hit.collider != null)
        {
            stick.GetComponent <AudioSource>().volume = Mathf.Lerp(0, 1, ReMap(dist, maxDist, minDist, 1, 0));
            stick.GetComponent <AudioSource>().Play();
            Vector3 forceDir = (ball.position - stick.position).normalized * -(dist - minDist - 0.02f) * forceMultiplier;
            ball.GetComponent <Rigidbody2D>().AddForce(forceDir, ForceMode2D.Impulse);
            follow = false;
            Invoke("HideShowStick", 0.2f);
        }

        // start following
        if (!follow)
        {
            if (ball.GetComponent <Rigidbody2D>().velocity.sqrMagnitude < 0.015f)
            {
                follow = true;
                selectionFx.GetComponent <Fader>().StartFade();
                Invoke("HideShowStick", 0.2f);
            }
        }
    } // update()