Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by Nosmo · Aug 24, 2017 at 03:52 PM · raycastraycastingdelaydoordoors

door that closes by itself

I'm trying make a door that opens with user input but then closes by itself a few second later using raycast NOT animation

Can anyone help?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by alexanderameye · Aug 24, 2017 at 03:57 PM

Hey! I have created this script for opening doors that you can download for free, it is well documented and is useful to read. It uses Raycasting to rotate a door!

https://www.assetstore.unity3d.com/en/#!/content/38694

For the automatic closing, you could create a trigger zone using a collider. On that trigger zone you then need to use a script that checks when the user has entered using the function 'OnColliderEnter' or 'OnColliderStay'. Using that function you can send a 'message' to your door script saying that it has to close the door. That's basically the method you could use to achieve the automatic closing of your door.

You could also use a timer instead of working with a trigger zone: see KillerOfSteam7's answer.

Comment
Add comment · Show 4 · 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 WinterboltGames · Aug 24, 2017 at 04:10 PM 0
Share

multiple doors using physics to open and close will harm the performance and please tell me 1 reason not to use Quaternions???

avatar image alexanderameye WinterboltGames · Aug 24, 2017 at 04:28 PM 1
Share

Could you please calm down.

The user requested a physics based approach ins$$anonymous$$d of animations. Assu$$anonymous$$g the user is creating a simple game it is very unlikely that he has a lot of doors in a single scene. I offered the user an approach using a trigger zone since that might provide some more flexibility than a timer, although a timer has less performance impact, a trigger zone certainly also has his benefit.

And why do you assume I am not using quaternions?

avatar image WinterboltGames alexanderameye · Aug 24, 2017 at 04:33 PM 1
Share

I didn't mean to be aggressive. sorry if I harassment you.

Show more comments
avatar image
0

Answer by WinterboltGames · Aug 24, 2017 at 04:03 PM

the easiest method I could think of is simply using Quaternion.Euler(x, y, z) and some coroutines like this...

 public float DoorOpenAngle = 90f; // the value of the y-axis when the door is open
 public float DoorClosedAngle = 0f; // the value of the y-axis when the door is closed
 public bool IsOpen = false;

 public IEnumerator AutoClose ()
 {
     yield return new WaitForSeconds (10f); // set it to whatever you like...
     transform.rotation = Quaternion.Euler(0f, DoorClosedAngle, 0f);
 }

 private void SwitchDoor ()
 {    
     if (!IsOpen)
     {            
         var _openRotation = Quaternion.Euler(0f, DoorOpenAngle, 0f);
         transform.rotation = _openRotation;
         IsOpen = true;        
     StartCoroutine(AutoClose()); // call these here and the door will close after 10 seconds...
     } else if (IsOpen)
     {    
         var _closedRotation = Quaternion.Euler(0f, DoorClosedAngle, 0f);
         transform.rotation = _closedRotation;
         IsOpen = false;    
     }    
 }

then call it with raycasting as follows...

 private void Update ()
 {
     var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var Hit = new RaycastHit()
 
     if (Physics.Raycast(Ray, out hit))
     {
         // you can do this with you own method of checking
         if (Hit.transform.gameObject.tag == "Door") 
         {
             Hit.transform.gameObject.SendMessage("SwitchDoor");
         }
     }    
 }


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

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

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

Related Questions

How to Fix a Guns Firing Script so it Doesn't Constantly Fire 3 Answers

How often do Raycasts check for collisions? 1 Answer

Is this way of making a raycast correct 2 Answers

Mouse Over Questions 1 Answer

Raycasting fail 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