• 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 gazza529 · Sep 17, 2013 at 09:49 AM · mouseclickdoor

door open/close on mouse click

hi i know there are many variations of this question but none of the answers work for me i want to open and close a door when you click on the door handle unfortunately while it opens i struggle to trigger the closed. heres my code hopefully just needs tweaking. ive had it working using keyboard functions but cant seem to use it for mouse click. many thanks

 var door : GameObject;
 var knob : GameObject;
 var smooth : float = 1.0;
 var open : boolean = false;
 var closed : boolean = true;
 
 
 function Start (){
 door = GameObject.Find("door");
 knob = GameObject.Find("Sphere");
 
 }
 
 function Update () {
 //to rotate the door to the open angle//
         if (open){
         var doorOpen = Quaternion.Euler(0, -80, 0);
         door.transform.rotation = Quaternion.Slerp(transform.rotation, doorOpen, Time.deltaTime * smooth);
         }
         //to rotate the door to the closed angle//
         if (closed){
         var doorClosed = Quaternion.Euler(0, 0, 0);
         door.transform.rotation = Quaternion.Slerp(transform.rotation, doorClosed, Time.deltaTime * smooth);
 }
 
 if (Input.GetMouseButtonDown(0)){
 var ray : Ray = Camera.main.ScreenPointToRay 
                             (Input.mousePosition);
         var hit : RaycastHit;
 //raytrace on door knob//
  if (knob.collider.Raycast (ray, hit, 10.0)&&closed ==true) {
 open = true;
 }  
  if (knob.collider.Raycast (ray, hit, 10.0)&&open ==true) {
 closed = true;
 }  
 
    //this should make sure that open and closed are opposites but doesnt seem to//
         if (open==true){
             closed = (open == false);
             }
 
 }
 }
Comment
Add comment · Show 1
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 instruct9r · Sep 17, 2013 at 11:22 AM 0
Share

Are you sure, that when the door opens, it's not too far away for the distance of the ray, to reach it?

First i think you shoud remove the last piece of code if (open == true) {....}

that might be breaking you'r functionality. Second, you can just use one variable (boolean) ins$$anonymous$$d of 2. Just use Open and put it to true or false, depending if the door is open or closed. This way if you want to make the opposite you can just write

 open = !open;

But you actually won't need that, if you use one variable...

Third: I think the Raycast condition have to be If - Else, ins$$anonymous$$d of If - If. Because when you open the door it immediately starts the other if and this won't happen if you use if-else condition. The one will skip the other...

I'll take a look at the actual code in Unity to see how it can be optimised..

1 Reply

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

Answer by instruct9r · Sep 17, 2013 at 12:35 PM

 #pragma strict
 
 var door : GameObject;
 var knob : GameObject;
 var smooth : float = 1.0;
 var open : boolean = false;
 
 
 
 function Start ()
 {
     door = GameObject.Find("door");
     knob = GameObject.Find("Sphere");
 }
  
 function Update () {
 //to rotate the door to the open angle//
        if (open)    // open = true
        {
               var doorOpen = Quaternion.Euler(0, 80, 0);    // Get the Euler of the angle that we want to reach
                var angle = Quaternion.Angle(transform.rotation, doorOpen);        // Convert it to angle
             door.transform.rotation = Quaternion.Slerp(transform.rotation, doorOpen, smooth * Time.deltaTime);
             
             // When we come near the angle, that we want to reach, we snap the door to that angle
             // cuz otherwise we will never reach it, because of the smoothed lerp of the Quaternion.
             // If the snap is too obvious try replacing the "smooth" with 0.5 for example.
             if (angle <= smooth)
                 door.transform.rotation = doorOpen;
        }
 
        //to rotate the door to the closed angle//
       else          //  open = false
        {
                doorOpen = Quaternion.Euler(0, 0, 0);
                angle = Quaternion.Angle(transform.rotation, doorOpen);
             door.transform.rotation = Quaternion.Slerp(transform.rotation, doorOpen, smooth * Time.deltaTime);
             
             if (angle <= smooth)
                 door.transform.rotation = doorOpen;
 
        }
  
     if (Input.GetMouseButtonDown(0))
     {
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         //raytrace on door knob//
          if (knob.collider.Raycast (ray, hit, 10.0) && open) 
          {
             open = false;
         }
         else
         {
             open = true;
         }
     }
 }
Comment
Add comment · Show 9 · 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 gazza529 · Sep 18, 2013 at 07:51 AM 0
Share

works perfectly thank you very much!!!!!!!

avatar image instruct9r · Sep 18, 2013 at 04:42 PM 0
Share

$$anonymous$$ark the question as answered ;)

avatar image gazza529 · Sep 23, 2013 at 12:56 PM 0
Share

just found a small problem. the knob collider is only effective for true or false not both. so when else = true a click anywhere on the screen opens the door and a click on the knob closes it. i dont suppose theres a workaround where the collider is the effector for both ways?

avatar image instruct9r · Sep 23, 2013 at 04:09 PM 0
Share

ok this is because the script is checking if the ray hit's the collider of the knob only to make the open false, otherwise it just executes it. $$anonymous$$y bad :)

Here's fixed version of the Input.Get$$anonymous$$ousebutton... code..

     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         //raytrace on door knob//
          if (knob.collider.Raycast (ray, hit, 10.0)) 
          {
              open = !open;
          }
     }
 }

open = !open - means that everytime you press the knob, open will become the opposite of open. If it's true = !true (false) and vice versa :)

cheers

avatar image gazza529 · Sep 24, 2013 at 07:45 AM 0
Share

excellent its perfect now thank you

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Door Sound Script Help 1 Answer

Can you only detect one collider when clicking a position with two overlapping colliders? 2 Answers

save and move to mouse click positions 1 Answer

Simple Checkpoint system 1 Answer

Presure plate that triggers an animation 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