コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            dragStartPosition   = cam.ScreenToWorldPoint(Input.mousePosition);
            dragStartPosition.z = 15;
        }

        if (Input.GetMouseButton(0))
        {
            Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            currentPoint.z = 15;

            lineTrajectory.RenderLine(dragStartPosition, currentPoint, slingshotPower, minPower, maxPower, rb, transform);
        }

        if (Input.GetMouseButtonUp(0))
        {
            lineTrajectory.EndLine();

            dragEndPosition   = cam.ScreenToWorldPoint(Input.mousePosition);
            dragEndPosition.z = 15;

            force = new Vector2(
                Mathf.Clamp(dragStartPosition.x - dragEndPosition.x, minPower.x, maxPower.x),
                Mathf.Clamp(dragStartPosition.y - dragEndPosition.y, minPower.y, maxPower.y)
                );

            AudioSource.PlayClipAtPoint(jumpSound, transform.position, jumpSoundVolume);
            Vector2 velocity = force * slingshotPower;

            //rb.AddForce(force * slingshotPower, ForceMode2D.Impulse);

            if (velocity.magnitude > velocityThreshold)
            {
                rb.velocity = velocity;
            }
        }
    }
コード例 #2
0
ファイル: DragNShoot.cs プロジェクト: syrexAT/physicproject
    private void Update()
    {
        if (MouseOverUICheck.BlockedByUI == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                startPoint   = cam.ScreenToWorldPoint(Input.mousePosition);
                startPoint.z = 15;
            }

            if (Input.GetMouseButton(0))
            {
                Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
                currentPoint.z = 15;
                lt.RenderLine(startPoint, currentPoint);
            }

            if (Input.GetMouseButtonUp(0))
            {
                endPoint   = cam.ScreenToWorldPoint(Input.mousePosition);
                endPoint.z = 15;

                force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
                float startEndPointX = startPoint.x - endPoint.x;
                float startEndPointY = startPoint.y - endPoint.y;
                velocity = new Vector3(Mathf.Clamp(startEndPointX, minPower.x, maxPower.x), Mathf.Clamp(startEndPointY, minPower.y, maxPower.y), 0f) * power;

                clone = Instantiate(physicsObjectCircle, startPoint, Quaternion.identity);
                clone.GetComponent <PhysicsBody>().velocity = velocity;
                PhysicsManager.instance.CheckforSelection(); //not fully implemented //To deselect every other clone before spawning the new one, so the newest spawned circle is always isSelected = true;
                clone.GetComponent <PhysicsBody>().isSelected = true;

                lt.EndLine();
            }

            //RECTANGLE
            //if (Input.GetMouseButtonDown(1))
            //{
            //    startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            //    startPoint.z = 15;
            //}

            //if (Input.GetMouseButton(1))
            //{
            //    Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            //    currentPoint.z = 15;
            //    lt.RenderLine(startPoint, currentPoint);

            //}

            //if (Input.GetMouseButtonUp(1))
            //{
            //    endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            //    endPoint.z = 15;

            //    force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
            //    float startEndPointX = startPoint.x - endPoint.x;
            //    float startEndPointY = startPoint.y - endPoint.y;
            //    velocity = new Vector3(Mathf.Clamp(startEndPointX, minPower.x, maxPower.x), Mathf.Clamp(startEndPointY, minPower.y, maxPower.y), 0f) * power;

            //    clone = Instantiate(physicsObjectRectangle, startPoint, Quaternion.identity);
            //    clone.GetComponent<PhysicsBody>().velocity = velocity;

            //    lt.EndLine();
            //}
        }
    }