• 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 Keenan_Pearce · Jun 25, 2012 at 11:57 AM · animationdoor

Point and Click Door Opening

Hello I'm a bit of a noob so go easy...

I'm trying to make a room escape game on unity, this is all I have so far...

alt text

For the first level shown above I just want to open the door when the mouse is looking at the door and the player clicks the left mouse button.

I've tried many times and different ways to do this but I cannot accomplish this :(

I have iTween Editor set up to play the animation of the door opening, which looks fine I just need a way of activating it to play the animation and load the next level once the door is opened.

I have this bit of code already, but its not much use...

 var target : GameObject;
 var eventName1 : String;
 var sound1 : AudioClip;

 function OnMouseEnter (){
 if (Input.GetMouseButtonDown(0)){
 iTweenEvent.GetEvent(target, eventName1).Play();
 audio.clip = sound1;
 audio.Play ();
 }
 }

 function OnMouseExit (){
 }

 function Update (){
 }

Does anyone know how to make this work? Thanks in advance!

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 Jessica_Ann · Jun 25, 2012 at 03:15 PM 0
Share

does your door object have at least a box collider on it? If it has no collider than the on mouse enter will never fire off

2 Replies

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

Answer by AlucardJay · Jun 25, 2012 at 04:08 PM

you could use a raycast method.

the door will need a collider : http://unity3d.com/support/documentation/Components/class-BoxCollider.html

then cast a ray (Raycast) : http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

if the raycast hit the door, then play animation. (raycast video) : http://www.unity3dstudent.com/2010/08/intermediate-i01-raycasting/

then you could write something like this :

 #pragma strict
 
 var target : GameObject;
 var eventName1 : String;
 var sound1 : AudioClip;
 
 function Update (){
     
     if (Input.GetMouseButtonDown(0)){
         
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         
         if (Physics.Raycast (ray, hit)) {
             
             // * For When the script is on the Door object
             //if(hit.collider.gameObject == this.transform.gameObject){
             
             // * For When the door is loaded as  target : GameObject;
             if(hit.collider.gameObject == target){
             
                 iTweenEvent.GetEvent(target, eventName1).Play();
                 audio.clip = sound1;
                 audio.Play ();
                 
                 // my door animation - for testing
                 //target.transform.parent.animation.Play("Door_Open");
             }
         }
     }
 }
 



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 Keenan_Pearce · Jun 25, 2012 at 07:59 PM 0
Share

Thanks that worked... sort of, when ever I play the game on unity it doesn't always open the door when clicked, I have to first $$anonymous$$imize unity and then open it back up for the door to open when I click.

I then built and run the game but that seems the have the same problem, also when I $$anonymous$$imize the browser I doesn't work (this is a browser based game), also this game has no character movement its just the scene as shown and you have to do certain puzzles to open the next level.

avatar image AlucardJay · Jun 26, 2012 at 03:46 AM 0
Share

That is very strange, not sure why you have that going on. The unity player in a web-page needs to be active to accept input, but clicking on the unity player should make it active anyway.

You could try some debugging to see what is going on.

put this line after

 function On$$anonymous$$ouseEnter (){
     Debug.Log("$$anonymous$$ouse Enter over Door");

put this line after

 if (Input.Get$$anonymous$$ouseButtonDown(0)){
     Debug.Log("L$$anonymous$$B pressed");

put this line after

 if (Physics.Raycast (ray, hit, 100)) {
     Debug.Log("L$$anonymous$$B pressed. Collider is " + hit.collider.gameObject.name);

this should create some output in the console window, showing when the mouse is over the door, when the L$$anonymous$$B is pressed and what collider the raycast hits. Run this (in Unity, not webplayer) without $$anonymous$$imizing etc to see if the script and the input is working (it should). on my way out but shall check back and test for myself later.

avatar image AlucardJay · Jun 26, 2012 at 07:47 AM 0
Share

I've had a look and it seems to be the On$$anonymous$$ouseEvent method. When the Raycast is in Update, it works fine. I have updated the answer, and changed the script so that it can be placed anywhere, just load your door in the Inspector. As long as the collider is on the var target : GameObject; this should work. tested =]

if this script is part of the player, you could load several 'targets' and use the same raycast to check different colliders at the same time :

 if(hit.collider.gameObject == target2){

target3 target4 FrontDoor $$anonymous$$itchenDoor etc

avatar image Keenan_Pearce · Jun 27, 2012 at 05:31 PM 0
Share

Omg thank you so much!

avatar image
0

Answer by Keenan_Pearce · Jun 27, 2012 at 08:41 PM

Omg thank you so much!!

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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I make animations snap to a frame? 1 Answer

Trigger Door Animation 2 Answers

How do I reverse a animation mid way? 1 Answer

How can I make the player collide with the door while the door is opening? 2 Answers

Start Animation in code is not working 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