Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 lorainiscool · Feb 24, 2014 at 06:13 PM · animationgameisland

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;
             }
         }
     }
 }
 
Comment
Add comment · Show 7
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 perchik · Feb 24, 2014 at 06:16 PM 0
Share

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#)

avatar image lorainiscool · Feb 24, 2014 at 06:27 PM 0
Share

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

avatar image perchik · Feb 24, 2014 at 06:29 PM 0
Share

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.

avatar image lorainiscool · Feb 24, 2014 at 06:35 PM 0
Share

I tried to use debug.log but i'm not getting any output in the log window.

avatar image perchik · Feb 24, 2014 at 06:36 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · 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 lorainiscool · Feb 24, 2014 at 06:50 PM 0
Share

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

avatar image perchik · Feb 24, 2014 at 06:52 PM 0
Share

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"
avatar image lorainiscool · Feb 26, 2014 at 06:39 PM 0
Share

Hi, i just checked it with the debug.log statements right now.... and suddenly its working perfectly fine!!!! thanks so much for you help!!!!

avatar image Jamora lorainiscool · Feb 26, 2014 at 06:39 PM 1
Share

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.

avatar image perchik · Feb 26, 2014 at 06:40 PM 0
Share

Hmm. Adding the Debug statements should not actually fix anything, it should just help you find the problem.

avatar image lorainiscool · Feb 27, 2014 at 03:10 PM 0
Share

I know... it was quite weird.... suddenly my graphics slowed down, but the door opened on collision and closed after 3 sec

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.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image

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

Play animation when colliding with trigger! 3 Answers

[SOLVED] Shooting fail (Due to camera setup) 2 Answers


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