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 SuperSparkplug · Jun 14, 2015 at 07:02 AM · dooropen

Door Opening Script isn't working.

Hi there,

Admittedly, I'm an artist, not a programmer, but I had a game made with some help from a programmer previously. I took a script I used for opening a door in my project from a year ago and adapted it to my current project. However, while I am able to make the parameters work, since I'm able to get the open variable to turn false and true on trigger, I can't seem to get the door I'm applying the script on to open. Furthermore, oddly enough, there are random select times when the game will freeze for a moment and the door will just appear open when I get near it. I can't control if or when that happens. I'm very confused and would like some help. Here's the script.

 using UnityEngine;
 using System.Collections;
 
 public class DoorOpenScript : MonoBehaviour {
 
     public float smooth = 3.50f;
     public float DoorOpenAngle = 90.0f;
     public bool open;
     //public bool opening = false;
     private bool enter;
     public Quaternion defaultRot;
     private Quaternion openRot;
 
     
     public GameObject CameraMarker;
     public Vector3 CamPosition;
     public bool spawnOnce = false;
     public AudioClip openEffect;
     
     //GUI Door Prompt Parameters
     public int buttonW = 120;
     public int buttonH = 45;
 
     public int GUIPromptCentre = 56;
 
     
     private bool unused = false;
     
     // Use this for initialization
     void Start ()
     {    
 
         defaultRot = transform.rotation;
         openRot = Quaternion.Euler (new Vector3 (defaultRot.eulerAngles.x,
                                                  defaultRot.eulerAngles.y + DoorOpenAngle, defaultRot.eulerAngles.z));
         open = false;
         
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         if (open == true) {
             //Open door
             transform.rotation = Quaternion.Slerp (transform.rotation, openRot, Time.deltaTime * smooth);
             open = true;
             
         } if(open == false) {
             //Close door
             transform.rotation = Quaternion.Slerp (transform.rotation, defaultRot, Time.deltaTime * smooth);
         }
         
 
     }
     
     void OnGUI ()
     {
         
         if (enter == true){
             GUI.Button (new Rect (Screen.width/2, Screen.height/2, buttonW, buttonH), 
                         "Test");
         }
     }
     
     //Activate the Main function when player is near the door
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.tag == "Player") {
             enter = true;
 
         }
         
     }
     
     //Deactivate the Main function when player is go away from door
     void OnTriggerExit (Collider other)
     {
         if (other.gameObject.tag == "Player") {
             enter = false;
         }
     }
     
     
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KingFriggy · Jun 14, 2015 at 01:56 PM

The variable 'enter' is set true/false in your trigger functions. While it is the open bool that is being tested in Update(). Change open to enter in Update().

Unless you are looking for the button to be pressed to open the door, then change the OnGUI() to:

if(enter) { if(GUI.Button(#parameters#)) { open = true; } }

Comment
Add comment · Show 3 · 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 SuperSparkplug · Jun 14, 2015 at 07:17 PM 0
Share

See, I did do that before I posted this here, but then I undoed alot because something broke. I did try it again and still no dice. I have no idea why.

avatar image SuperSparkplug · Jun 14, 2015 at 08:53 PM 0
Share

Ah, okay. $$anonymous$$y bad. I tried your method again but the reason it wasn't working is because I messed with the open rotation number in the Inspector. I set it back to 90 and now it works fine. Thanks! :)

That being said, there is an issue that it twitches because it's based on the player's proximity to the door and the trigger box collider in front of the door moves with it. So, if the player stops in the middle of it opening or moves backwards, it will twitch like crazy. Any way around that?

avatar image KingFriggy · Jun 15, 2015 at 12:27 AM 0
Share

So the collider moves as the door rotates.

$$anonymous$$ake an empty object as a child to the door and add the trigger collider to it. Attach the above script to the empty object.

$$anonymous$$ake sure to change all the transforms to transform.parent in the script, as the door is now the parent.

And to keep the collider's rotation constant stick this into the update:

transform.rotation = Quaternion.Euler(new Vector3(0,0,0));

$$anonymous$$ake sure the empty object's rotation is zeroed out by default. Hope this helps!

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

22 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 avatar image avatar image avatar image avatar image

Related Questions

How to pick up battery to open door? 2 Answers

Script for a door that requires (9 papers) to open 0 Answers

Opening a door with a key 2 Answers

How to open a gate with a key ? 1 Answer

Trying to open a door so far then stop 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