Scaling model by clicking and dragging issue

Hi, I’ve been trying all day to figure out an issue with scaling an object based the location of the mouse while it’s clicking and dragging.

I have 2 main things goin on.
1 - Parent of 2 objects is being resized based on the distance of the camera. This gives the illusion of the object being the same size no matter the distance of the camera.
2 - You can click and drag one of the children which resizes the other child object. I want the child object to be resized to reach the object that is being dragged.

So down to the actual issue. The object when being resized either goes too long or too short depending on how big the parent is.

Here is a quick video of the issue:

Here are the project files: https://www.dropbox.com/s/2fr3zkllbjdb3a0/ScalingIssueProject - 20119.2.11.f1.zip?dl=0

Scene setup is like so:

  • Camera - Free flight script attached
  • Direction Light
  • Ship - GameObject used as a parent or container - This has the issue script attached
  • Hull - Child - Cube - This is the object that is scaled when the nose is dragged
  • Nose - Child - Capsule - This is what the user clicks and drags to resize the Hull

Here is the script that is attached to the Ship GameObject:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test_Script : MonoBehaviour
{
    public GameObject hull;
    public GameObject end;

    //Scale Relative to Camera
    public Camera cam;
    public float objectScale = 1.0f;
    private Vector3 initialScale;


    Vector3 mouseOffset;

    bool isTransform;

    void Start()
    {
        // record initial scale, use this as a basis
        initialScale = transform.localScale;

        // if no specific camera, grab the default camera
        if (cam == null)
            cam = Camera.main;
    }

    private void Update()
    {
        // scale object relative to distance from camera plane stuff --------------- Comment out to remove relative scaling >
        Plane plane = new Plane(cam.transform.forward, cam.transform.position);
        float dist = plane.GetDistanceToPoint(transform.position);
        transform.localScale = initialScale * dist * objectScale;
        //----------------------------------------------------------------- Comment out to remove relative scaling <

        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject == end.gameObject)
                {
                    GameObject selectedPart = hit.collider.gameObject;

                    //Calculate initial mouse to object distance
                    float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
                    Vector3 mouseTo3D = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
                    mouseOffset = mouseTo3D - gameObject.transform.position;
                    isTransform = true;
                }
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            isTransform = false;
        }

        if (isTransform)
        {
            float distance_to_screen = Camera.main.WorldToScreenPoint(transform.position).z;
            Vector3 mouseTo3D = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));

            //Move Nose
            Vector3 newPos = new Vector3(mouseTo3D.x, transform.position.y, transform.position.z);
            end.transform.position = newPos;
            
           //Scale Hull along x axis
            float xScale = mouseTo3D.x - transform.position.x;
            Vector3 newScale = new Vector3(xScale, hull.transform.localScale.y, hull.transform.localScale.z);
            hull.transform.localScale = newScale;
            hull.transform.position = new Vector3((mouseTo3D.x - hull.transform.localScale.x) / 2, transform.position.y, transform.position.z);
        }
    }
}

Any help would be great!!

Thanks

You are trying to scale up a GameObject with those models inside right?
Edit: if this is right you have to make a gameobject array and scale it with a foreach.
Tip: Use the new input system :wink:

1 Like

Hey Thanks for the reply. Sorry its not the parent object scaling that is the issue. The parent object as a whole scales based on the distance it is from the camera.
It’s the scaling of the object as i am clicking and dragging gthat is the issue. This object is basicaly going to be used as a gizmo, but specifically for space ships. This is why it scales based on the distance from the camera.

I have these lines of code that should scale the entire object and its children based on the distance from the camera. As shown in the video it makes the object appear the same size regardless of the camera distrance.

Plane plane = new Plane(cam.transform.forward, cam.transform.position);
        float dist = plane.GetDistanceToPoint(transform.position);
        transform.localScale = initialScale * dist * objectScale;

These lines of code move the “nose” GameObject to the position of the mouse on the objects x axis.

//Nose
            Vector3 newPos = new Vector3(mouseTo3D.x, transform.position.y, transform.position.z);
            end.transform.position = newPos;

The following lines of code should scale the hull object up to the length of where the mouse is. But as shown in the video depending on the scale of the object as a whole the length is either too long or too short.

float xScale = mouseTo3D.x - transform.position.x;
            Vector3 newScale = new Vector3(xScale, hull.transform.localScale.y, hull.transform.localScale.z);
            hull.transform.localScale = newScale;
            hull.transform.position = new Vector3((mouseTo3D.x - hull.transform.localScale.x) / 2, transform.position.y, transform.position.z);

Thankss

I have included the project files in the thread and its only like 10mb. Maybe you can see something I can’t.

*** I have reworded the origional post as it was nonsence.***

Here is another quick scene I have made so it looks more like the axis of a gizmo.
At first you see the camera move closer and further away from the object. This is to demonstrate that even though I am moving around the gizmo is the same relative size on screen.
Then I show what happens when you click and drag on the gizmo.
You can see that the thin red line is either too long or too short when its being resized. When clicking and dragging the thin red line is the only thing being resized. But I can’t figure out how to base that length on the current scale of the parent object.

The specific lines of code that needs to be fixed in this are:

float xScale = mouseTo3D.x - transform.position.x; //<---- I need to some how Compensate scale of the parent in to this
Vector3 newScale = new Vector3(xScale, hull.transform.localScale.y, hull.transform.localScale.z);

I can’t for the life of me figure out how to compensate for the scale of parent which is:

transform.localScale = initialScale * dist * objectScale;

As the video shows the object scaling works when moving around. it’s the scaling on the xaxis when it is being dragged that i am having an issue with.

Any help would be pretty awesome. I’ve wiped and restarted about 3 times and you’re currently looking at the 4th restart which seams to be the most promising, it’s just this last part giving trouble.

Thanks

I wasnt sure about the array as i thought having to loop through each potential object it while doing other stuff would be a nuisance but it proves to be the solution.

Thanks!!!