1
\$\begingroup\$

Despite an hour or so of Googling I can't seem to figure out how to calculate the scale factor based on distance. Here's the illustration of my problem.

enter image description here

I've tried manipulating the x values and such, but until now I still cannot figure it out. Here's the snippet:

Vector3 v3Scale = frontWall.transform.localScale;
frontWall.transform.localScale = new Vector3(v3Pos.x*2.0f , v3Scale.y , v3Scale.z);

Any ideas? Thanks in advance!

UPDATE:

if(getTarget.CompareTag("LeftRightWall") && getTarget.transform.childCount == 1)
        {
            float translate = getTarget.transform.position.x - getTarget.transform.GetChild(0).gameObject.transform.position.x;
            Debug.Log("value :"+translate);
            foreach (GameObject frontWall in GameObject.FindGameObjectsWithTag("FrontWall"))
            {
                Vector3 v3Scale = frontWall.transform.localScale;
                float scaleFactor = translate/frontWall.transform.localScale.x;
                frontWall.transform.localScale = new Vector3(translate , v3Scale.y , v3Scale.z);
            }
            v3Pos.z = getTarget.transform.position.z;
            v3Pos.y = getTarget.transform.position.y;
            getTarget.transform.position = v3Pos;
            getTarget.transform.GetChild(0).gameObject.transform.position = new Vector3(-v3Pos.x, v3Pos.y, v3Pos.z);
        }   

The LeftRightWall is the ones that I drag on the picture, with another vertical line as its child. The FrontWall is the horizontal line that is stretched when the LeftRightWall is dragged. Hope its clear enough. Thanks

UPDATE: This is the closest one from what I'm trying to achieve... ((

enter image description here

Here's the code:

float translate = getTarget.transform.position.x - getTarget.transform.GetChild(0).gameObject.transform.position.x;
            Debug.Log("value :"+translate);
            foreach (GameObject frontWall in GameObject.FindGameObjectsWithTag("FrontWall"))
            {
                Vector3 v3Scale = frontWall.transform.localScale;
                frontWall.transform.localScale = new Vector3(translate+v3Pos.x, v3Scale.y , v3Scale.z);
            }

UPDATE: This is what I'm able to do so far, thanks to the answer below. However I still don't know why the other side of the panel is behaving like that. The scale of horizontal lines also shrinks when I click on the vertical line.

enter image description here

This is the updated code:

ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        float dist;
        plane.Raycast(ray, out dist);
        Vector3 v3Pos = ray.GetPoint(dist); 
        if(getTarget.CompareTag("LeftRightWall") && getTarget.transform.childCount == 1)
        {
            float translate = getTarget.transform.localPosition.x - getTarget.transform.GetChild(0).gameObject.transform.position.x;
            Debug.Log("value :"+getTarget.transform.GetChild(0).gameObject.transform.localPosition.x);
            foreach (GameObject frontWall in GameObject.FindGameObjectsWithTag("FrontWall"))
            {
                Vector3 v3Scale = frontWall.transform.localScale;
                float scaleFactor = (translate/v3Scale.x);
                frontWall.transform.localScale = new Vector3(v3Scale.x*scaleFactor, v3Scale.y, v3Scale.z);
            }
            v3Pos.z = getTarget.transform.localPosition.z;
            v3Pos.y = getTarget.transform.localPosition.y;
            getTarget.transform.localPosition = v3Pos;
            getTarget.transform.GetChild(0).gameObject.transform.position = new Vector3(-v3Pos.x, v3Pos.y, v3Pos.z);
        }   
\$\endgroup\$
1
\$\begingroup\$

Maybe this math could guide you if as I understand your problem is that the horizontal and vertical lines "split" and you want them to stick together.

Having the distance between the two upper points of vertical lines, you can get the distance between them. Let's say d1.

The horizontal length of your upper line is length.

Then, d1 / length will give you the scale factor you need to "achieve" d1.

Numeric example:

distance = 10
length = 8

scaleFactor = 10/8 = 1.25

Then, 8 * 1.25 = 10.

I'm not completely sure this is what you expect, if it's not, please update your question with more details.


EDIT: In your updated code, I don't see your scaleFactor been used, your frontWall scale depends on translate, not on the factor. Besides that, this scaleFactor is calculated with scale.

What I tried to tell you to do is something more like the following:

Vec3 translate = positionX - other.positionX; //That's ok
float scaleFactor = translate.transform.x / frontWall.lengthX;
//Here, it's meant to use the length of the frontWall in its X-axis
Vec3 newScale = new Vec3(frontWall.lengthX * scaleFactor, localScale.y, localScale.z);

Excuse me for this code not being compilable, I've not been using Unity for a while now and I'm on my Smartphone.

\$\endgroup\$
  • \$\begingroup\$ Yes you are correct, I did try your math, but I still got the same result like before. I don't know what is happening though... \$\endgroup\$ – Mira Aug 22 '17 at 9:48
  • \$\begingroup\$ Could you update the question with more details, please? So I can see how you do and use this math, where v3Scale and frontWall come from, etc. \$\endgroup\$ – jjimenezg93 Aug 22 '17 at 9:50
  • \$\begingroup\$ Already updated the question, hope it's clear to you what I am trying to achieve. \$\endgroup\$ – Mira Aug 22 '17 at 9:57
  • \$\begingroup\$ Edited my answer \$\endgroup\$ – jjimenezg93 Aug 23 '17 at 5:54
  • \$\begingroup\$ I tried your code, I able to make it accurate but only on the right side of the wall, I put them in my updated question. \$\endgroup\$ – Mira Aug 23 '17 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.