- Home /
Outpost door animation not triggered on collision
I am working on the survival island game from the Unity 3.x Game Development Essentials by Will Goldstone. I can't seem to get the door to open or play the sound when the first person controller collides with the door. I have followed the script given in the book but can't seem to find the issue. I am attaching my code here. Someone please help!!
using UnityEngine;
using System.Collections;
public class PlayerCollisions : MonoBehaviour
{
bool doorIsOpen = false;
float doorTimer = 0.0f;
public float doorOpenTime = 3.0f;
public AudioClip doorOpenSound;
public AudioClip doorShutSound;
void OnControllerCollidorHit(ControllerColliderHit hit)
{
if(hit.gameObject.tag == "playerDoor" && doorIsOpen == false)
{
currentDoor = hit.gameObject;
OpenDoor(currentDoor);
}
}
void OpenDoor(GameObject door)
{
doorIsOpen = true;
door.audio.PlayOneShot(doorOpenSound);
door.transform.parent.animation.Play("dooropen");
}
void ShutDoor(GameObject door)
{
doorIsOpen = false;
door.audio.PlayOneShot (doorShutSound);
door.transform.parent.animation.Play ("doorshut");
}
// Use this for initialization
/*void Start ()
{
}
*/
GameObject currentDoor;
// Update is called once per frame
void Update ()
{
if(doorIsOpen)
{
doorTimer += Time.deltaTime;
if(doorTimer > doorOpenTime)
{
ShutDoor(currentDoor);
doorTimer = 0.0f;
}
}
}
}
How have you attempted to debug it? Have you put in Debug.Log statements to see if it's getting into the collision or the OpenDoor() functions? What about Debug.Log statements in the if(doorIsOpen) condition?
If I remember correctly, part of your problem may be that you are trying to initialize variables at the global scope ins$$anonymous$$d of in the Start function...
bool doorIsOpen = false;
float doorTimer = 0.0f;
public float doorOpenTime = 3.0f;
public AudioClip doorOpenSound;
public AudioClip doorShutSound;
Declare all of those variables there, but assign initial values in Start.
(Although, I'm not entirely sure that's a problem in C#)
Umm... I am quite new to this so I'm not really sure how to do the Debug.Log thing. I tried changing the location of declaration of variables as you suggested. Still no use
Simple, inside of any function in Unity you can add
Debug.Log("message");
and whatever you put will be added to the Debug Log window in Unity. so you could do like
void OpenDoor(GameObject door)
{
Debug.Log("opening door");
doorIsOpen = true;
door.audio.PlayOneShot(doorOpenSound);
door.transform.parent.animation.Play("dooropen");
}
and then if/when unity calls that function, it would print out "opening door" in the Log window, so you could see it actually got there.
I tried to use debug.log but i'm not getting any output in the log window.
That suggests that it's probably not getting into that function.
I'd uncomment Start, put a debug.log statement there, just to make sure you're using debug.log correctly.
Then add debug statements into the collision function and into the if statement on the collision function.
That way, you can start to trace what your program is actually doing, ins$$anonymous$$d of just hoping it works.
Answer by perchik · Feb 24, 2014 at 06:45 PM
So now, (after the above comments), you've figured out that your game is never getting into the OnControllerCollidorHit
function, so you should look up that function.
When you copy your function into google to get to the Unity Docs for it, you should realize that you've misspelled it and it should be "OnControllerColliderHit" with an e not an o in collider.
Whether that actually solves your problem or not, I'm not sure, but it's a clear starting point for what's wrong.
Well, that didn't solve much problem other than showing me that the control has actually entered the function but has not gotten into the if condition...
Before the if statment you could add:
Debug.Log("Tag is" + hit.gameObject.tag); //to see if it equals player
and
Debug.Log("isDoorOpen?" + doorIsOpen); //to see if it prints out "False"
Hi, i just checked it with the debug.log statements right now.... and suddenly its working perfectly fine!!!! thanks so much for you help!!!!
Please use the very small "add new comment" button to add comments. Answers are meant to be solutions to the question.
If you found an answer particularly helpful, do mark it as correct by clicking on the checkmark below the thumbs.
Hmm. Adding the Debug statements should not actually fix anything, it should just help you find the problem.
I know... it was quite weird.... suddenly my graphics slowed down, but the door opened on collision and closed after 3 sec
Your answer
Follow this Question
Related Questions
I dont get the new animation window 0 Answers
Making an animation play when you press down 2 keys 1 Answer
A node in a childnode? 1 Answer