- Home /
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...
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!
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
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");
}
}
}
}
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.
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.
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