Search Unity

been working on a door, any help?

Discussion in 'Scripting' started by light766, Oct 1, 2011.

  1. light766

    light766

    Joined:
    Apr 23, 2009
    Posts:
    250
    for days i have been trying to make this door open and close on click and i failed at EVERYTHING. i tried animation from unity but it still rotates in middle which i dont want, i want it to move from like a hinge, so what i did was i made a script that rotates around a object, and well the problem with that was when it went past 360 and when it went below 0.

    Any suggestions or tutorials to get this thing out of the way? :confused:
     
  2. eTag96

    eTag96

    Joined:
    Oct 24, 2010
    Posts:
    110
    in a modeling app like 3ds max I think you should be able to configure the pivot point of the door.
     
  3. scratch250

    scratch250

    Joined:
    Mar 28, 2011
    Posts:
    91
    just animate it in 3ds max and then export it over in unity :)
     
  4. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    Transform.RotateAround

    function RotateAround (point : Vector3, axis : Vector3, angle : float) : void
    Description

    Rotates the transform about axis passing through point in world coordinates by angle degrees.

    This modifies both the position and the rotation of the transform

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour {
    6. void Update() {
    7. transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    8. }
    9. }
    10.  

    so if the doors height is parallel with Vector3.up (y axis) then it should be

    transform.RotateAround( DOORS, Vector3.up, 20 * Time.deltaTime);

    where DOORS is Vector3 variable and you should find doors "pivot" with it, it is point in which unity will make vertical axis (Vector3.up) around which will doors rotate. so that means you need to bring that point at the edge of doors

    after that you need to set borders so doors don't rotate full round, just 90° you can do it with time or angle check...
     
    Last edited: Oct 1, 2011
  5. light766

    light766

    Joined:
    Apr 23, 2009
    Posts:
    250
    Well this is what i have right now, but it doesnt seem to work..

    Hinge is a empty gameobject by the way

    Code (csharp):
    1.  
    2. var smooth = 2.0;
    3. var DoorOpenAngle = 85.0;
    4. var DoorCloseAngle = 0.0;
    5. var startPosition = 0;
    6. var open : boolean;
    7. var enter : boolean;
    8. var rotateAround : Transform;
    9.  
    10. function Start(){
    11.  
    12. startPosition = transform.eulerAngles.y;
    13. if(transform.eulerAngles.y > 264  transform.eulerAngles.y < 300){
    14. transform.eulerAngles.y = 265;
    15. startPosition = 265;
    16. }
    17. if(transform.eulerAngles.y > 300){
    18. transform.eulerAngles.y = 0;
    19. startPosition = 0;
    20. }
    21.  
    22.  
    23. }
    24.  
    25. //Main function
    26. function Update (){
    27.  
    28.  if(open == true){
    29.   //var target = Quaternion.Euler (0, DoorOpenAngle, 0);
    30.   // Dampen towards the target rotation
    31.   //transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
    32.   //Time.deltaTime * smooth);
    33.   if(rotateAround.transform.eulerAngles.y < DoorOpenAngle + startPosition){
    34.   transform.RotateAround (rotateAround.position, Vector3.up, Time.deltaTime * smooth);
    35.   }
    36.  }
    37.  
    38.  if(open == false){
    39.  // var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
    40.   // Dampen towards the target rotation
    41.   if(transform.eulerAngles.y > 3 + DoorCloseAngle + startPosition){
    42.   transform.RotateAround (rotateAround.position, Vector3.up, Time.deltaTime * -smooth);
    43.   //transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
    44.  // Time.deltaTime * smooth);
    45.  }
    46.  }
    47.  
    48. }
    49.  
    50.  
    51. //Activate the Main function when player is near the door
    52. function activate(){
    53.  
    54. if(open){
    55. open = false;
    56. }else{
    57. open = true;
    58. }
    59.  
    60. }
    61.  
    62.  
    63.