x


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!

more ▼

asked Jun 25 '12 at 11:57 AM

Keenan_Pearce gravatar image

Keenan_Pearce
2 1 1 2

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

Jun 25 '12 at 03:15 PM Jessica_Ann
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");
         }
       }
    }
}
more ▼

answered Jun 25 '12 at 04:08 PM

alucardj gravatar image

alucardj
13.6k 34 55 86

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 minimize 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 minimize 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.

Jun 25 '12 at 07:59 PM Keenan_Pearce

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 OnMouseEnter (){
    Debug.Log("Mouse Enter over Door");

put this line after

if (Input.GetMouseButtonDown(0)){
    Debug.Log("LMB pressed");

put this line after

if (Physics.Raycast (ray, hit, 100)) {
    Debug.Log("LMB 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 LMB is pressed and what collider the raycast hits. Run this (in Unity, not webplayer) without minimizing 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.

Jun 26 '12 at 03:46 AM alucardj

I've had a look and it seems to be the OnMouseEvent 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 KitchenDoor etc

Jun 26 '12 at 07:47 AM alucardj

Omg thank you so much!

Jun 27 '12 at 05:31 PM Keenan_Pearce
(comments are locked)
10|3000 characters needed characters left

Omg thank you so much!!

more ▼

answered Jun 27 '12 at 08:41 PM

Keenan_Pearce gravatar image

Keenan_Pearce
2 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3768
x192
x3

asked: Jun 25 '12 at 11:57 AM

Seen: 913 times

Last Updated: Jun 27 '12 at 08:41 PM