Hello, community!
I am currently using Unity to create some 3D representations of some tools and objects. These scenes allow the user to rotate the object, zoom in and out, and turn off/on any animations it might have.
Also, these objects are usually made up of multiple child objects that make up the innards and such. What I am looking to do is find the distance from the center of the parent object to each individual child object, and then transform.position the child objects by 2x that. The result would be to give off an “expanded view” of the inner workings.
Here is my current code in C#:
void Start()
{
cameraZoom = (maxDistance + minDistance) / 2; //Set camera start distance in the middle of min and max range
Camera.main.orthographicSize = cameraZoom;
savedLocalPos = transform.localPosition;
}
void Update()
{
if (!automate)
HandleInputs();
else if(automate)
Automate();
if (disassemble)
Disassemble();
else if (!disassemble)
transform.localPosition = savedLocalPos;
if(Input.GetAxis("Mouse ScrollWheel") != 0)
cameraZoom = Mathf.Clamp(cameraZoom - Input.GetAxis("Mouse ScrollWheel") * 2,
minDistance, maxDistance);
Camera.main.orthographicSize = cameraZoom;
CalculateObjectRotation();
}
void HandleInputs()
{
if(Input.GetMouseButton(1))
{
xDeg -= Input.GetAxis("Mouse X") * speed;
zDeg -= Input.GetAxis("Mouse Y") * speed;
}
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
xDeg -= Input.GetAxis("Horizontal") * speed;
zDeg -= Input.GetAxis("Vertical") * speed;
}
}
void Automate()
{
xDeg -= speed * speedMod / 20;
yDeg += speed * speedMod;
zDeg = 0;
}
void Disassemble()
{
transform.localPosition = savedLocalPos * 2;
}
void OnGUI()
{
GUI.skin = customSkin;
if(!automate)
{
if(GUI.Button(new Rect(5,5,110,25), "Engage") && !automate)
automate = true;
}else
{
if(GUI.Button(new Rect(5,5,110,25), "Disengage") && automate)
automate = false;
speedMod = GUI.HorizontalSlider(new Rect(Screen.width/2 - 150, Screen.height - 50, 300, 100), speedMod, speedModMin, speedModMax);
GUI.Box(new Rect(Screen.width/2 - 50, Screen.height - 37, 100, 25),"Speed = " + speedMod.ToString("F2") +"x");
}
if(!disassemble)
{
if(GUI.Button(new Rect(5,35,110,25), "Disassemble") && !disassemble)
disassemble = true;
}else
{
if(GUI.Button(new Rect(5,35,110,25), "Assemble") && disassemble)
disassemble = false;
}
if(GUI.Button(new Rect(5,65,110,25), "Help"))
{
//still working on this pop-up window
}
if(GUI.Button(new Rect(5,95,110,25), "Reset Rotation"))
{
if(automate)
{
xDeg = 0f;
} else xDeg = yDeg = zDeg = 0f;
}
cameraZoom = GUI.VerticalSlider(new Rect(Screen.width - 12, 5, 100, Screen.height - 55), cameraZoom, minDistance, maxDistance);
}
void CalculateObjectRotation()
{
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,zDeg);
transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
}
}