Quantcast
Channel: Latest Questions by Spikeh
Viewing all articles
Browse latest Browse all 16

Calculating Door Hinge Pivots

$
0
0
I'm trying to move an empty game object to a hinge position on a door, so I can use it as the pivot point to swing the door open. The door script I've written has a setting which allows the player to open it in multiple different ways; slide it left / right / up / down, or swing it in / out on either the left or right hinge. Initially I ran into problems trying to get the slide movements working correctly depending on the axis the door was aligned on, but I managed to work that one out. Now I'm having a few issues with the swing movements. This method is executed in the Start() method of my door script: public Transform Pivot { get; private set; } /// /// Creates an empty gameobject that can be used as the door's pivot point when swinging open / closed /// private void SetUpDoorPivot() { var doorPivot = new GameObject(string.Concat(name, "_pivot")); // Move the pivot object to the door's pivot point, depending on the swing type // By default, just set the pivot to the device's world location var pivotPosition = transform.position; // TODO: Calculate the bottom hinge position for each swing type switch (_doorTransitionType) { case TransitionType.SwingAwayLeftHinge: pivotPosition = renderer.bounds.min; // add depth break; case TransitionType.SwingAwayRightHinge: pivotPosition = renderer.bounds.max; break; case TransitionType.SwingTowardsLeftHinge: pivotPosition = renderer.bounds.min; break; case TransitionType.SwingTowardsRightHinge: pivotPosition = renderer.bounds.max; // add depth break; } doorPivot.transform.position = pivotPosition; // Assign the pivot object to the same parent as the door device doorPivot.transform.parent = transform.parent; // Set the door device's parent to the pivot object transform.parent = doorPivot.transform; // Save the pivot for later use Pivot = doorPivot.transform; } Everything works expected, except the hinges don't sit in the right place on the door; I get spurious results depending on which axis the door is aligned on (X or Z), and which swing type I have set. All of my doors are currently just cubes that have been resized - they have a considerable depth at the moment, in order to better see where the hinges are. Here are some quick screenshots (gizmos are displayed where the pivot object is): ![alt text][1] ![alt text][2] I can't upload any more pics, but you hopefully get the idea. As you can see, I'm creating the pivot at runtime, and making it the parent of the door object. This all works fine - however, the final positioning performed in the switch statement is where I'm falling. I'm struggling to get my head around the bounds object - I'm not sure what each property refers to (I have experimented), and the unity documentation is a bit sparse on the subject. At the moment, the SwingAwayRightHinge and SwingTowardsLeftHinge pivot is put in a valid location for doors that are aligned on the world X axis (though both pivots are not placed at the bottom of the object - which isn't too much of a problem, as I'm rotating around the Y axis anyway), but not on any other axis. The other pivots are placed on the opposite hinge, meaning that the door clips into the rest of the scenery. This is what I'm trying to avoid, by calculating the relevant pivot point at runtime. The main issue is that when I rotate the doors to another axis (see the door on the left in the above pic), the hinge positions change for each setting. I know what I have to do, but I'm having issues working out how I do it! Any help will most definitely be appreciated - even if you can provide me with some tutorials, white papers or blog posts on bounds and how to calculate vertices. [1]: /storage/temp/19336-doorpivots-notworking-closed.png [2]: /storage/temp/19337-doorpivots-notworking-open.png

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>