- Home /
Can't rotate a parent object by selecting the child
I have a script that handles mouse click object selection & drag rotation for some cylinders. These cylinders have a child cube attached to it. The click & drag rotation works on the cylinders if I select the cylinder directly, but not the child cube.
I have some code that assigns the transform to the platform variable like so:
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
if (hitInfo.transform.gameObject.tag == "Rotatable")
{
this.platform = hitInfo.transform; // this works
}
if (hitInfo.transform.gameObject.tag == "Stoppable")
{
Debug.Log("Hit child, rotate parent: " + hitInfo.transform.parent.name);
this.platform = hitInfo.transform.parent; // this doesn't
}
}
The weird thing is that the debug message outputs the correct name of the parent transform. I want to be able to rotate the cylinder the exact same way when I click and drag the cube as if I'm clicking and dragging the cylinder.
Answer by acornies · Jul 11, 2014 at 06:04 AM
Turns out, this code works as is after some refactoring around the object (platform) selection. Before, I had attached this script to the parent game object itself and NOT the child which prevented it from working. After attaching the same script to the child object, things started working, but it wasn't ideal as I didn't want the script to be overused.
I now have only one empty game object with this script attached that selects the objects (with appropriate tag) based on touch position. Hopefully, this helps someone out in some way.
Answer by tswalk · Jul 11, 2014 at 06:10 AM
perhaps you need to complete the reference to:
this.platform = hitInfo.transform.parent.transform;
.. not sure, just a thought.
Your answer
Follow this Question
Related Questions
How would you set a camera lerp position with code not a child object? 1 Answer
2D Click Anywhere on Physics Object and Drag 0 Answers
Just updated to 2017.3.0f3 And my moving platforms are locking my character's rotation 1 Answer
Flip over an object (smooth transition) 3 Answers
Click and Drag rotation similar to Scene View Gizmos 0 Answers