Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Spikeh · Dec 14, 2013 at 04:52 PM · doorboundspivothinge

Calculating Door Hinge Pivots

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; }
 
 /// <summary>
 ///     Creates an empty gameobject that can be used as the door's pivot point when swinging open / closed
 /// </summary>
 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 alt text

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.

doorpivots-notworking-closed.png (80.3 kB)
doorpivots-notworking-open.png (119.3 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _MGB_ · Dec 14, 2013 at 05:39 PM 0
Share

Can't see your pics, but it may be the scale of the door object affecting things. Can you try making the scaled visual door mesh a child of the door object so it has a uniform scale of 1?

avatar image Spikeh · Dec 14, 2013 at 07:11 PM 0
Share

Fixed the pictures. Also found this, that seriously helps to explain how unity provides and deals with the bounds property on different types of objects:

http://raypendergraph.wikidot.com/unity-developer-s-notes#toc10

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Spikeh · Dec 15, 2013 at 03:43 PM

Well, after much testing, and many experiments, I've managed to sort this out myself. I've had to use a combination of the door's forward / right vectors, then multiply them by the mesh's extents, and use the localScale to resize the mesh appropriately. Here's the finished code, for anyone who might hit the same issue:

 /// <summary>
 ///     Creates an empty gameobject that can be used as the door's pivot point when swinging open
 /// </summary>
 private void SetUpDoorPivot() {
     var doorPivot = new GameObject(string.Concat(name, "_pivot"));
     var gizmo = doorPivot.AddComponent<CircleGizmo>();
     gizmo.Size = .10f;
     gizmo.Color = new Color().RandomiseChannels();

     // 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 center
     var pivotPosition = collider.bounds.center;

     var mesh = GetComponent<MeshFilter>().mesh;
     var depth = transform.right * (mesh.bounds.extents.x * transform.localScale.x);
     var direction = transform.forward * (mesh.bounds.extents.z * transform.localScale.z);

     switch (_doorTransitionType) {
         case TransitionType.SwingAwayLeftHinge:
             pivotPosition -= (direction + depth);
             break;
         case TransitionType.SwingAwayRightHinge:
             pivotPosition += (direction - depth);
             break;
         case TransitionType.SwingTowardsLeftHinge:
             pivotPosition -= (direction - depth);
             break;
         case TransitionType.SwingTowardsRightHinge:
             pivotPosition += (direction + depth);
             break;
     }

     doorPivot.transform.localPosition = 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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GraviterX · Dec 14, 2013 at 05:33 PM

Hello, I recently also made a door. I used this simple script to make a door. It allows you to press F to open a door using a trigger. You can rotate it any way to make it suitable. Here is the script: //Make an empty game object and call it "Door" //Rename your 3D door model to "Body" //Parent a "Body" object to "Door" //Make sure thet a "Door" object is in left down corner of "Body" object. The place where a Door Hinge need be //Add a box collider to "Door" object and make it much bigger then the "Body" model, mark it trigger //Assign this script to a "Door" game object that have box collider with trigger enabled //Press "f" to open the door and "g" to close the door //Make sure the main character is tagged "player"

 // Smothly open a door
 var smooth = 2.0;
 var DoorOpenAngle = 90.0;
 var AudioFile : AudioClip;
 private var open : boolean;
 private var enter : boolean;
 
 private var defaultRot : Vector3;
 private var openRot : Vector3;
 
 function Start(){
 defaultRot = transform.eulerAngles;
 openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
 }
 
 //Main function
 function Update (){
 if(open){
 //Open door
 transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
 }else{
 //Close door
 transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
 }
 
 if(Input.GetKeyDown("f") && enter){
 open = !open;
 }
 }
 
 function OnGUI(){
 if(enter){
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
 }
 }
 
 //Activate the Main function when player is near the door
 function OnTriggerEnter (other : Collider){
 if (other.gameObject.tag == "Player") {
 enter = true;
 }
 }
 
 //Deactivate the Main function when player is go away from door
 function OnTriggerExit (other : Collider){
 if (other.gameObject.tag == "Player") {
 enter = false;
 }
 }
 //@youtube.com/user/maksimum654321
 //www.warmerise.com - check this game

Hope this helps!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Spikeh · Dec 14, 2013 at 07:15 PM 0
Share

Thanks - that's not really what I was asking for, but it's appreciated anyway! $$anonymous$$y doors are considerably more complex than a standard door, as they are only one part of a full compliment of interactable objects. Look at them as a construction kit!

This is a problem with accessing bounds on a game object in order to ascertain a dynamic pivot, depending on the animation type selected in the unity inspector editor.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate door around pivot with physics 1 Answer

Adding child repositions parent tranform 1 Answer

Please help finding center of object 1 Answer

Mesh.RecalculateBounds 1 Answer

Best way to rotate a door 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges