コード例 #1
0
    void Update()
    {
        //if slingshot is not in aimingMode, dont run
        if (!aimingMode)
        {
            return;
        }

        //Get the current mouse position in 2D screen coordinates
        Vector3 mousePos2D = Input.mousePosition;

        mousePos2D.z = -Camera.main.transform.position.z;
        Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);

        //Find delta from the launchPos to the mousePos3D
        Vector3 mouseDelta = mousePos3D - launchPos;
        //Limit mouseDelta to the radius of the slingshot Spherecollider
        float maxMagnitude = this.GetComponent <SphereCollider>().radius;

        if (mouseDelta.magnitude > maxMagnitude)
        {
            mouseDelta.Normalize();
            mouseDelta *= maxMagnitude;
        }

        //move the projectile to this new position
        Vector3 projPos = launchPos + mouseDelta;

        projectile.transform.position = projPos;

        if (Input.GetMouseButtonUp(0))
        {
            //the mouse has been released
            aimingMode = false;
            projectileRigidBody.isKinematic = false;
            projectileRigidBody.velocity    = -mouseDelta * velocityMult;
            FollowCam.POI = projectile;
            projectile    = null;
            MissionDemolition.Shotsfired();
            ProjectileLine.S.poi = projectile;
        }
    }