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