Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animating a door

Discussion in 'Scripting' started by Yourking77, Jun 17, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Animating a door with an actual animation did not work because the box collider would not follow the animation. So I have had to rethink my method and create a script for it. this should rotate the door when you click the mouse, I will add raycasts and make it go a little more smoothly later but for now I need to figure out how to get this script working. I do not see anything wrong with it and cannot figure out why this will not work.

    The random variable should not really be affecting this either I did the same basic thing with my flower and tree randomizer script. So i am sure that works. I only added that in there to make the game feel more alive, it makes it so you do not always open a door to the exact same position. and the second line to make the door move should swing it back a little to make it sort of bounce.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HouseDoor01 : MonoBehaviour {
    6.  
    7.     //The door we will be rotation.
    8.     public GameObject houseDoor01;
    9.  
    10.     //Sets the door position.
    11.     bool doorOpen = false;
    12.     public bool openByDefault = false;
    13.  
    14.     //the min and max values the door will use to move and bounce on its hindges.
    15.     public float doorMinOpen = 95.0f;
    16.     public float doorMaxOpen = 105.0f;
    17.  
    18.     public float doorBounce = 10.0f;
    19.  
    20.     void Start() {
    21.         //Opens the door if it is supposed to be opened by default.
    22.         if (openByDefault == true) {
    23.             var randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    24.             houseDoor01.transform.Rotate(0, 0, randomDoorRotation - doorBounce);
    25.         }
    26.     }
    27.  
    28.     void Update () {
    29.         if (Input.GetMouseButtonDown(0)) {
    30.             if (doorOpen == true) {
    31.                 CloseDoor();
    32.             }
    33.             else if (doorOpen == false) {
    34.                 OpenDoor();
    35.             }
    36.         }
    37.     }
    38.  
    39.     void OpenDoor() {
    40.         //Randomly rotates the door, to make the game a little more unique.
    41.         var randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    42.         houseDoor01.transform.Rotate(0, 0, randomDoorRotation);
    43.  
    44.         //Swing the door back a little after it is opened to make it sort of bounce.
    45.         houseDoor01.transform.Rotate(0, 0, randomDoorRotation - doorBounce);
    46.         doorOpen = true;
    47.     }
    48.  
    49.     void CloseDoor() {
    50.         houseDoor01.transform.Rotate(0, 0, 0);
    51.         doorOpen = false;
    52.     }
    53. }
     
    Last edited: Jun 17, 2016
  2. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Found one problem, I cleared the armature and all animation data from the mesh and that made it rotate, but it rotates oddly and never returns to the default position.

    I am in the process of figuring out Quaternions and am making progress but any help is still appreciated. I also have a few unused variables, I was planning on making this script re-usable for chests and such and also need to rotate the y axis for that, so that is why there are a few unused variables.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HouseDoor01 : MonoBehaviour {
    6.  
    7.     //The door we will be rotation.
    8.     [Header("Door")]
    9.     public GameObject houseDoor01;
    10.  
    11.     //Sets the door position.
    12.     [Header("Open by default")]
    13.     bool doorOpen = false;
    14.     public bool openByDefault = false;
    15.  
    16.     //The min and max values the door will use to move and bounce on its hindges.
    17.     float doorDefaultAngleX = 0.0f;
    18.     float doorDefaultAngleY = 0.0f;
    19.     float doorDefaultAngleZ = 0.0f;
    20.  
    21.     [Header("Open angle")]
    22.     public float doorMinOpen = 95.0f;
    23.     public float doorMaxOpen = 105.0f;
    24.  
    25.     public float doorBounce = 10.0f;
    26.  
    27.     [Header("Smooth transition")]
    28.     public float doorSmooth = 2.0f;
    29.  
    30.     //Tells teh door where to go.
    31.     private Quaternion doorToRotate;
    32.  
    33.     void Start() {
    34.         //Sets the default doors roatations for when we close it.
    35.         doorDefaultAngleX = houseDoor01.transform.localRotation.x;
    36.         doorDefaultAngleY = houseDoor01.transform.localRotation.y;
    37.         doorDefaultAngleZ = houseDoor01.transform.localRotation.z;
    38.  
    39.         //Set the Quaternion that will control the door.
    40.         Quaternion doorToRotate = houseDoor01.transform.localRotation;
    41.  
    42.         //Opens the door if it is supposed to be opened by default.
    43.         if (openByDefault == true) {
    44.             var randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    45.             houseDoor01.transform.Rotate(0, 0, randomDoorRotation - doorBounce);
    46.         }
    47.     }
    48.  
    49.     void Update () {
    50.         //Keeps the door in the same place as the Quaternion.
    51.         houseDoor01.transform.localRotation = doorToRotate;
    52.  
    53.         //Rotate the door to the desired position.
    54.         if (Input.GetMouseButtonDown(0)) {
    55.             if (doorOpen == true) {
    56.                 //Close the door if it is open.
    57.                 CloseDoor();
    58.             }
    59.             else if (doorOpen == false) {
    60.                 //Open the door if it is closed.
    61.                 OpenDoor();
    62.             }
    63.         }
    64.     }
    65.  
    66.     void OpenDoor() {
    67.         //Controls the random rotations of the door.
    68.         var randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    69.  
    70.         //Randomly rotates the door, to make the game a little more unique.
    71.         var houseDoor01 = Quaternion.Euler(0, randomDoorRotation, 0);
    72.         transform.localRotation = Quaternion.Slerp(transform.localRotation, houseDoor01, Time.deltaTime * doorSmooth);
    73.  
    74.         //Swing the door back a little after it is opened to make it sort of bounce.
    75.         houseDoor01 = Quaternion.Euler(0, randomDoorRotation - doorBounce, 0);
    76.         transform.localRotation = Quaternion.Slerp(transform.localRotation, houseDoor01, Time.deltaTime * doorSmooth);
    77.  
    78.         //Keeps track of the door when it is open.
    79.         doorOpen = true;
    80.     }
    81.  
    82.     void CloseDoor() {
    83.         //Closes the door to it's default close position.
    84.         var houseDoor01 = Quaternion.Euler(0, doorDefaultAngleZ, 0);
    85.         transform.localRotation = Quaternion.Slerp(transform.localRotation, houseDoor01, Time.deltaTime * doorSmooth);
    86.  
    87.         //Keeps track of the door when it is closed.
    88.         doorOpen = false;
    89.     }
    90. }
     
    Last edited: Jun 17, 2016
  3. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    I would take a look at this, I would suggest using a coroutine.
     
  4. BrendanKZN

    BrendanKZN

    Joined:
    Jun 22, 2011
    Posts:
    157
    Just a suggestion regarding the animated version of a door. Could you not animate an empty gameobject, call it a hinge and child the door to that? Otherwise I like your slightly randomized door openings. GL.
     
    infinitypbr likes this.
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I see you came back to the scripting side of things. I simplified the code to get it working. you where setting a GameObject houseDoor01 to get the x y z rotations of the door, but then in your OpenDoor() and ClosedDoor() you where using the local transform. so that told me you where setting this script on the door. so need to set the Gameobject to it's self. I didn't work on the door bounce. the default x y z that you storing where not used. you keep over writing them. so that have been why the door never returned to the closed position 0,0,0
    I set the variables to public for my testing. feel free to put them back to private



    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class HouseDoor01 : MonoBehaviour
    7. {
    8.     //The door we will be rotation.
    9.  
    10.     [Header("Door")]
    11.     //public GameObject houseDoor01;
    12.  
    13.     //Sets the door position.
    14.     [Header("Open by default")]
    15.     public bool doorOpen = false;
    16.     public bool openByDefault = false;
    17.  
    18.  
    19.     [Header("Close angle")]
    20.     //public float doorDefaultClosedAngleX;
    21.     //public float doorDefaultClosedAngleY;
    22.     //public float doorDefaultClosedAngleZ;
    23.  
    24.     [Header("Open angle")]
    25.     public float doorMinOpen = 95.0f;
    26.     public float doorMaxOpen = 105.0f;
    27.     public float doorBounce = 10.0f;
    28.  
    29.     [Header("Smooth transition")]
    30.     public float doorSmooth = 0.01f;
    31.  
    32.     //Tells the door where to go.
    33.     //public Quaternion doorToRotate;
    34.     //public Quaternion doorFromRotate;
    35.     public Transform from;
    36.  
    37.     public float randomDoorRotation;
    38.  
    39.  
    40.     void Start()
    41.     {
    42.         if (doorOpen)
    43.         {
    44.             from = transform;
    45.             from.transform.Rotate(0, 90, 0);
    46.         }
    47.         else
    48.         {
    49.             from = transform;
    50.             from.transform.Rotate(0, 0, 0);
    51.         }
    52.      
    53.      
    54.         //Sets the default doors roatations for when we close it.
    55.         //doorDefaultClosedAngleX = houseDoor01.transform.localRotation.x;
    56.         //doorDefaultClosedAngleY = houseDoor01.transform.localRotation.y;
    57.         //doorDefaultClosedAngleZ = houseDoor01.transform.localRotation.z;
    58.  
    59.         //Set the Quaternion that will control the door.
    60.         //Quaternion doorToRotate = houseDoor01.transform.localRotation;
    61.  
    62.         //Opens the door if it is supposed to be opened by default.
    63.         if (openByDefault == true)
    64.         {
    65.             randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    66.             from = transform;
    67.             doorOpen = true;
    68.             //houseDoor01.transform.Rotate(0, 0, randomDoorRotation - doorBounce);
    69.         }
    70.     }
    71.  
    72.  
    73.     void Update()
    74.     {
    75.         //Rotate the door to the desired position.
    76.         if (Input.GetMouseButtonDown(0))
    77.         {
    78.             if (doorOpen == true)
    79.             {
    80.                 //set the currect rotation position
    81.                 from = transform;
    82.                 doorOpen = false;
    83.             }
    84.             else
    85.             {
    86.                 //Controls the random rotations of the door.
    87.                 randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    88.                 //set the currect rotation position
    89.                 from = transform;
    90.                 doorOpen = true;
    91.             }
    92.         }
    93.  
    94.         if (doorOpen == true)
    95.         {
    96.             //Close the door if it is open.
    97.             OpenDoor();
    98.         }
    99.         else if (doorOpen == false)
    100.         {
    101.             //Open the door if it is closed.
    102.             CloseDoor();
    103.         }
    104.     }
    105.  
    106.     void OpenDoor()
    107.     {
    108.         transform.rotation = Quaternion.Slerp(from.rotation, Quaternion.Euler(0, randomDoorRotation, 0), Time.time * doorSmooth);
    109.      
    110.         //you'll need to add more code to get this part working
    111.         //Swing the door back a little after it is opened to make it sort of bounce.
    112.         //houseDoor01 = Quaternion.Euler(0, randomDoorRotation - doorBounce, 0);
    113.         //transform.localRotation = Quaternion.Slerp(transform.localRotation, houseDoor01, Time.deltaTime * doorSmooth);
    114.     }
    115.  
    116.  
    117.     void CloseDoor()
    118.     {
    119.         //Closes the door to it's default close position.
    120.         transform.rotation = Quaternion.Slerp(from.rotation, Quaternion.Euler(0, 0, 0), Time.time * doorSmooth);
    121.     }
    122. }
    123.  
     
  6. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Sorry everyone for the late reply, I was gone fishing for the weekend and did not get a chance to look at this thread at all.

    I will look into those thank you for the tip.

    That is a good idea that I had not thought of, however i cleared the animation data already and creating a more modular script may benefit me more in the long run if I can just set that script to everything that need to open and set what axis and how much it needs to rotate. I am glad everyone is not calling me an idiot for the randomization part actually, I was just looking through my tree randomization thing for references on vector3's and thought I might try that XD.

    Thank you for the script, you have been very helpful with my project lately and I appreciate it. I will take a look at what you did and compare it to mine and let you know what I can come up with tomorrow.
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    to make this script more universal, you need to create a public function to set the dorrOpen bool. and then set that bool via the raycast hit script you created last week. unless you want every click of the mouse to open and close the door.

    Code (CSharp):
    1. public void SetOpenDoorState()
    2. {
    3.     if(openDoor == true)
    4.     {
    5.         openDoor = false;
    6.     }else
    7.     {
    8.         openDoor = true;
    9.     }
    10. }
     
  8. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Okay thanks for that I will keep it in mind, but didn't I already have that in my script? what is the difference between mine and yours?
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    there is a lot of changes. the update function controls most of it. you have the animation functions where resetting themselves on every frame. with mine they only update with the onClick. Then the animation functions work.
    a big change was transform.localRotation is now transform.rotation
     
  10. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I just do not understand what I am doing wrong here, can you take a look at my new code and tell me what I can do to fix it? I cannot get a smooth transition despite having the slerp thing on there and it really screws up if the smooth time is too small and making it bigger does nothing. on top of that I have no idea how to begin making it sort of bounce back, from what I see on your script mine should be working, the only thing that is different is I am setting my coordinates of where the door should be in Qauternions and you are just setting the coordinates manually, which to make it more modular I would like to try and keep my way. Any suggestions or things you see wrong?

    I reset the coordinates on the mesh now in unity everything is at a rotation of 0,0,0 which makes it way easier to rotate, and I made the door almost work I just cannot get the slerp thing functioning.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HouseDoor01 : MonoBehaviour {
    6.  
    7.     //The door we will be rotating.
    8.     [Header("Door")]
    9.     public GameObject houseDoor01;
    10.  
    11.     //Sets the door position.
    12.     [Header("Open by Default")]
    13.     bool doorOpen = false;
    14.     public bool openByDefault = false;
    15.  
    16.     [Header("Open Axis")]
    17.     public bool doorXAxis = false;
    18.     public bool doorYAxis = true;
    19.     public bool doorZAxis = false;
    20.  
    21.     [Header("Open Angle")]
    22.     public float doorMinOpen = -95.0f;
    23.     public float doorMaxOpen = -105.0f;
    24.  
    25.     public float doorBounce = -10.0f;
    26.  
    27.     [Header("Smooth Transition")]
    28.     public float doorSmooth = 1.0f;
    29.  
    30.     //Tells the door where to go.
    31.     Quaternion defaultPosition;
    32.     Quaternion rotatePosition;
    33.     Quaternion bouncePosition;
    34.  
    35.     void Start() {
    36.         //Sets the default Quaternion that will control the door.
    37.         defaultPosition = houseDoor01.transform.rotation;
    38.  
    39.         //Opens the door if it is supposed to be opened by default.
    40.         if (openByDefault == true) {
    41.             //Controls the random rotations of the door.
    42.             var randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    43.  
    44.             //Opens the door when the game starts
    45.             if (doorXAxis == true) {
    46.                 houseDoor01.transform.rotation = Quaternion.Euler(randomDoorRotation - doorBounce, defaultPosition.y, defaultPosition.z);
    47.             }
    48.             if (doorYAxis == true) {
    49.                 houseDoor01.transform.rotation = Quaternion.Euler(defaultPosition.x, randomDoorRotation - doorBounce, defaultPosition.z);
    50.             }
    51.             if (doorZAxis == true) {
    52.                 houseDoor01.transform.rotation = Quaternion.Euler(defaultPosition.x, defaultPosition.y, randomDoorRotation - doorBounce);
    53.             }
    54.  
    55.             //Keeps track of the door when it is open.
    56.             doorOpen = true;
    57.         }
    58.     }
    59.  
    60.     void OnTriggerStay() {
    61.         //Rotate the door to the desired position.
    62.         if (Input.GetMouseButtonDown(0)) {
    63.             if (doorOpen == true) {
    64.                 //Close the door if it is open.
    65.                 CloseDoor();
    66.             }
    67.             else if (doorOpen == false) {
    68.                 //Open the door if it is closed.
    69.                 OpenDoor();
    70.             }
    71.         }
    72.     }
    73.  
    74.     void OpenDoor() {
    75.         //Controls the random rotations of the door.
    76.         var randomDoorRotation = Random.Range(doorMinOpen, doorMaxOpen);
    77.  
    78.         if (doorXAxis == true) {
    79.             //Randomly changes the rotation value based on a Min and Max value.
    80.             rotatePosition = houseDoor01.transform.rotation;
    81.             rotatePosition = Quaternion.Euler(randomDoorRotation, defaultPosition.y, defaultPosition.z);
    82.  
    83.             //Randomly rotates the door, to make the game a little more unique.
    84.             houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, rotatePosition, Time.time * doorSmooth);
    85.  
    86.             //Sets the doors bounce based on the current opening position.
    87.             //bouncePosition = houseDoor01.transform.rotation;
    88.             //bouncePosition = Quaternion.Euler(randomDoorRotation - doorBounce, defaultPosition.y, defaultPosition.z);
    89.  
    90.             //Swing the door back a little after it is opened to make it sort of bounce.
    91.             //houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, bouncePosition, Time.deltaTime * doorSmooth);
    92.         }
    93.  
    94.         if (doorYAxis == true) {
    95.             //Randomly changes the rotation value based on a Min and Max value.
    96.             rotatePosition = houseDoor01.transform.rotation;
    97.             rotatePosition = Quaternion.Euler(defaultPosition.x, randomDoorRotation, defaultPosition.z);
    98.             //Randomly rotates the door, to make the game a little more unique.
    99.             houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, rotatePosition, Time.time * doorSmooth);
    100.  
    101.             //Sets the doors bounce based on the current opening position.
    102.             //bouncePosition = houseDoor01.transform.rotation;
    103.             //bouncePosition = Quaternion.Euler(0, randomDoorRotation - doorBounce, 0);
    104.  
    105.             //Swing the door back a little after it is opened to make it sort of bounce.
    106.             //houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, bouncePosition, Time.deltaTime * doorSmooth);
    107.         }
    108.  
    109.         if (doorZAxis == true) {
    110.             //Randomly changes the rotation value based on a Min and Max value.
    111.             rotatePosition = houseDoor01.transform.rotation;
    112.             rotatePosition = Quaternion.Euler(defaultPosition.x, defaultPosition.y, randomDoorRotation);
    113.  
    114.             //Randomly rotates the door, to make the game a little more unique.
    115.             houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, rotatePosition, Time.time * doorSmooth);
    116.  
    117.             //Sets the doors bounce based on the current opening position.
    118.             //bouncePosition = houseDoor01.transform.rotation;
    119.             //bouncePosition = Quaternion.Euler(defaultPosition.x, defaultPosition.y, randomDoorRotation - doorBounce);
    120.  
    121.             //Swing the door back a little after it is opened to make it sort of bounce.
    122.             //houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, bouncePosition, Time.deltaTime * doorSmooth);
    123.         }
    124.  
    125.         //Keeps track of the door when it is open.
    126.         doorOpen = true;
    127.     }
    128.  
    129.     void CloseDoor() {
    130.         //Closes the door to it's default close position.
    131.         houseDoor01.transform.rotation = Quaternion.Slerp(houseDoor01.transform.rotation, defaultPosition, Time.time * doorSmooth);
    132.  
    133.         //Keeps track of the door when it is closed.
    134.         doorOpen = false;
    135.     }
    136. }
     
  11. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I decided to completely redo my script based on some things I figured out and now it will not move at all, however I am almost certain that aside from the update void thing this script should be cleaner and should work a little better not sure, but I need help getting the door to move now.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class HouseDoor01 : MonoBehaviour {
    6.  
    7.     //The door we will be rotating.
    8.     [Header("Door")]
    9.     public GameObject houseDoor01;
    10.  
    11.     //Sets the door position.
    12.     [Header("Open by Default")]
    13.     bool doorOpen = false;
    14.     public bool openByDefault = false;
    15.  
    16.     [Header("Open Axis")]
    17.     public bool doorXAxis = false;
    18.     public bool doorYAxis = true;
    19.     public bool doorZAxis = false;
    20.  
    21.     [Header("Open Angle")]
    22.     float randomRotation = 0.0f;
    23.     public float doorMinOpen = -95.0f;
    24.     public float doorMaxOpen = -105.0f;
    25.  
    26.     public float doorBounce = -10.0f;
    27.  
    28.     [Header("Smooth Transition")]
    29.     public float doorSmooth = 1.0f;
    30.  
    31.     //Tells the door where to go.
    32.     Transform defaultPosition;
    33.     Transform currentPosition;
    34.     Transform desiredPosition;
    35.     Transform rotatePosition;
    36.     Transform bouncePosition;
    37.  
    38.     void Start() {
    39.         //Set the default door position
    40.         defaultPosition.transform.rotation = houseDoor01.transform.rotation;
    41.         currentPosition.transform.rotation = houseDoor01.transform.rotation;
    42.         desiredPosition.transform.rotation = houseDoor01.transform.rotation;
    43.  
    44.         //Open the door if it is set to open by default.
    45.         if (openByDefault == true) {
    46.             rotatePosition.transform.rotation = Quaternion.Euler(randomRotation, defaultPosition.transform.rotation.y, defaultPosition.transform.rotation.z);
    47.             houseDoor01.transform.rotation = rotatePosition.transform.rotation;
    48.  
    49.             //Set the door to open.
    50.             doorOpen = true;
    51.         }
    52.     }
    53.  
    54.     void Update() {
    55.         //Smoothly open the door if it is not in the desired position
    56.         if (doorXAxis == true) {
    57.             if (houseDoor01.transform.rotation.x != desiredPosition.transform.rotation.x) {
    58.                 swing();
    59.             }
    60.         }
    61.         else if (doorYAxis == true) {
    62.             if (houseDoor01.transform.rotation.y != desiredPosition.transform.rotation.y) {
    63.                 swing();
    64.             }
    65.         }
    66.         else if (doorZAxis == true) {
    67.             if (houseDoor01.transform.rotation.z != desiredPosition.transform.rotation.z) {
    68.                 swing();
    69.             }
    70.         }
    71.     }
    72.  
    73.     void OnTriggerStay() {
    74.         if (Input.GetMouseButtonDown(0)) {
    75.             if (doorOpen == false) {
    76.                 if (doorXAxis == true) {
    77.                     //Generates a random number for the doors rotation angle.
    78.                     RandomRotationMethod();
    79.  
    80.                     //Sets the desired position based on the generated angle.
    81.                     rotatePosition.transform.rotation = Quaternion.Euler(randomRotation, defaultPosition.transform.rotation.y, defaultPosition.transform.rotation.z);
    82.                     desiredPosition.transform.rotation = rotatePosition.transform.rotation;
    83.                     currentPosition.transform.rotation = houseDoor01.transform.rotation;
    84.  
    85.                     //Set the door to open.
    86.                     doorOpen = true;
    87.                 }
    88.                 else if (doorYAxis == true) {
    89.                     //Generates a random number for the doors rotation angle.
    90.                     RandomRotationMethod();
    91.  
    92.                     //Sets the desired position based on the generated angle.
    93.                     rotatePosition.transform.rotation = Quaternion.Euler(defaultPosition.transform.rotation.x, randomRotation, defaultPosition.transform.rotation.z);
    94.                     desiredPosition.transform.rotation = rotatePosition.transform.rotation;
    95.                     currentPosition.transform.rotation = houseDoor01.transform.rotation;
    96.  
    97.                     //Set the door to open.
    98.                     doorOpen = true;
    99.                 }
    100.                 else if (doorZAxis == true) {
    101.                     //Generates a random number for the doors rotation angle.
    102.                     RandomRotationMethod();
    103.  
    104.                     //Sets the desired position based on the generated angle.
    105.                     rotatePosition.transform.rotation = Quaternion.Euler(defaultPosition.transform.rotation.x, defaultPosition.transform.rotation.y, randomRotation);
    106.                     desiredPosition.transform.rotation = rotatePosition.transform.rotation;
    107.                     currentPosition.transform.rotation = houseDoor01.transform.rotation;
    108.  
    109.                     //Set the door to open.
    110.                     doorOpen = true;
    111.                 }
    112.             }
    113.             else if (doorOpen == true) {
    114.                 //Sets the desired position based on the default position.
    115.                 desiredPosition.transform.rotation = defaultPosition.transform.rotation;
    116.                 currentPosition.transform.rotation = houseDoor01.transform.rotation;
    117.  
    118.                 //Set the door to open.
    119.                 doorOpen = false;
    120.             }
    121.         }
    122.     }
    123.  
    124.     void swing() {
    125.         houseDoor01.transform.rotation = Quaternion.Slerp(currentPosition.transform.rotation, desiredPosition.transform.rotation, Time.time * doorSmooth);
    126.     }
    127.  
    128.     void RandomRotationMethod() {
    129.         //Generates a random number for the doors rotation angle.
    130.         randomRotation = Random.Range(doorMinOpen, doorMaxOpen);
    131.     }
    132. }
     
    Last edited: Jun 21, 2016
  12. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Hi YourKing,
    Can you tell me what gameobject you are dropping this script onto. I'm going to work on this tonight.
    I see you've added an onTriggerStay(). So that must mean your player stands in front of the door to allow the click to open and close it. I will build a scene and show you how to get the raycast to open and close the door. because I believe the click is not a good way to go about doing this. Keep a look out for my next replay.
     
  13. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I really did not want to trouble you it seemed so simple at first but at this point I am glad to hear you are willing to help me, my blender file is basically a square wall tile with a square hole in it and then a door filling that hole, I am importing it to unity and using that door and attaching that to the script, the doors origin is where the hinges are and the walls origin is at the center of the grid, I made sure that when I imported it all the rotations in unity are 0, 0, 0. I put the script on the blender object not the actual door, unlike most people from what I have seen.

    I wanted to go into detaill on that to sort of paint a picture of what I am doing because I want to try to make this script so I can use it on anything that needs to open. chests, doors ect. therefore all axis need to be rotatable in the script and you need to choose which axis rotates as well as be able to assign the door to rotate because that way I do not have to position everything just right in unity I can do it all in blender.. I am sure I am over complicating this like I was with my out of bounds script.
     
    Last edited: Jun 21, 2016
  14. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Here you go, enjoy, If you need more help, maybe we can skype or something
     

    Attached Files:

  15. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Thank you, I appreciate the help.
     
  16. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I have gotten it working finally thank you johne5, however raycasting is still a concern for me, is there some way maybe to have a script that casts a ray and then have every other script use that ray that was cast? right now the way you have it seems kind of costly as far as the processing goes and everything? is this going to be a problem if I keep doing it that way for say hundreds of different prefabs and such?

    Edit: I almost ran into something I could not handle but I ended up replacing all the rotations with localRotations and it fixed the problem, is there a reason you were not using localRotation?
     
    Last edited: Jun 22, 2016
  17. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Yourking, I've update the script so the raycast returns what ever it hit to the object. each door not checks the raycast to see if it a match. This seams more costly to me. the last script I sent you was one raycast, that raycast checked if it was a match, but now you're going to have a raycast being fired by each gameObject. this will cause 100+ raycast to fire at the same time. So I would keep use the first script. but I've attached an updated script so you could see how it would look.

    The .localRotation thing I don't really care about. as long as I works. I've always used .rotation
     

    Attached Files:

  18. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    my game is going to be huge, one giant game world, with hundreds of objects of varying types and countless intractable game objects all in one scene, think of my game as like one of Bethesdas games, I used to mod those so I am getting ideas from those games. if I do it the way you gave me first it just seems that having a separate script for everything that needs it would be more costly and less modular. I will take a look at the new script, (a new script is not exactly what I was asking for) but i was just asking if it would be less efficient your way in the long run?

    I have almost no knowledge of those things but to me it seems your way would be inefficient because it is looking for an object with the name door, to make that work on everything that needs it I would have to create a new script for every item with a different name and function, wouldn't a few hundred separate scripts for every single interactive GameObject be very inefficient. now I am probably exaggerating and overthinking this because I know I would not need a different script for every object I could just name all the items the same thing and that would work I think.

    my idea was have one ray in a script and attach it to the camera, then have every single object use that ray in their own script, wouldn't that only make it one ray because all the other objects would be using that ray, right? or would they be creating a copy of that ray? I will take a look at the new script and see what you did but I may not use it until I run into problems with the current system that I cannot fix.
     
  19. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you really don't understand and i'm starting to give up. The size of game you're talking about will could have a hundred scripts easy.

    you have this. it's RayCaster.cs just place it on the camera. done

    The fist script works in this fashion. The raycast will access the public function on the object it hit. my script is calling a function on the Door.sc script, this door script can be renamed to something more generic o work with more objects. like WasHit.cs Do you know how to extend a class?

    The second script will do this. witch is bad, way too many raycasts.
     
  20. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is an updated scene with class extended.
    rotate doors and change colors. with the raycast on the camera. and only using 1 raycast per click.
     
  21. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    forgot to attach
     

    Attached Files:

  22. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Thanks for these, I appreciate them, though I made a simple mistake earlier, that I have no idea how I made. I was making seperate scripts for every object that needed a raycast, when I can just very easily put them all in the same script. I can do some pretty stupid things at night sometimes. so there goes all of my concerns. I fixed it now.

    the door swings very slowly at the end and I googled some and that is because i was using slerp. but when i googled how to make a door that is all that came up, so I am sort of disappointing with google right now and am gonna call it quits on the door for now, it is good enough the bounce just takes too long to take effect and it looks sort of funny. so thank you for all the help, hopefully I can figure out the inventory and ui systems pretty easily and not have t resort to unity forums for a while. my simple mistakes are piling up and I am making a fool of myself XD