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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ferbco · Dec 19, 2012 at 07:50 PM · raycastbug

This doormanager script from unity 3x Game Development Essentials is supposed to raycast the door open but it doesn't. Why not? Code now attached.

I have included two scripts I typed out from unity 3x game development essentials, that are supposed to demonstrate raycasting a door open only when the player object is facing it. Instead it seems to loop the sound of the door opening and closing very quickly, till it sounds like a radio with bad transmission, and the only animation you get to see is of the door closing when you stop facing it. Would someone please point out the script error(s) in the attachment - I want the door to open and close properly. Please note I have reimported all the assets, the console is error free, and all code is in javascript.

Thank you in advance

Thank you for pointing out attachments are not there. So following is the code of the door that is supposed to open and close the door once when triggered by a raycast modifier in another javascript:

 private var doorIsOpen : boolean = false;
 private var doorTimer : float = 0.0f;
 var doorOpenTime : float = 3.0f;
 var doorOpenSound : AudioClip;
 var doorShutSound : AudioClip;
 
 function Start() {
     doorTimer = 0.0f;
 }
 
 function Update() {
     if(doorIsOpen) {
         doorTimer += Time.deltaTime;
         
         if(doorTimer > doorOpenTime) {
             Door(doorShutSound, false, "doorshut");
             doorTimer = 0.0f;
         }
     }
 }
 
 function DoorCheck() {
     if(!doorIsOpen) {
         Door(doorOpenSound, true, "dooropen");
     }
 }
 
 function Door(aClip : AudioClip, openCheck : boolean, animName : String) {
     audio.PlayOneShot(aClip);
     doorIsOpen = openCheck;
     transform.parent.gameObject.animation.Play(animName);
 }

And here is the raycast javascript that is supposed to trigger it once:

 private var currentDoor : GameObject;
 
 function Update() {
     var hit : RaycastHit;
     
     if(Physics.Raycast (transform.position, transform.forward, hit, 3)) {
         if(hit.collider.gameObject.tag=="playerDoor"){
             currentDoor = hit.collider.gameObject;
             currentDoor.SendMessage("DoorCheck");
         }    
     }
 }
Comment
Add comment · Show 2
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 Dave-Carlile · Dec 19, 2012 at 08:56 PM 0
Share

Not seeing any code or attachments.

avatar image ferbco · Dec 20, 2012 at 06:28 AM 0
Share

Code there now

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DuxDucis · Dec 19, 2012 at 11:40 PM

You're probably doing it each time the raycast is true, which will start the animation each frame you're looking at the door, so you should either try holding a boolean of whether the door is opening and check before making the raycast, or use animation.PlayQueued

Comment
Add comment · Show 1 · 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 ferbco · Dec 20, 2012 at 06:44 AM 0
Share

The script has a door checking boolean function which is supposed to prevent the raycast from unceasingly triggering the door. It doesn't register console errors, but it just doesn't open and close the door once like it is supposed to. Can someone please point out the error in the code (now included above)?

avatar image
0

Answer by Nubz · Dec 20, 2012 at 02:52 AM

Keep reading Will shows 3 ways to handle the door and the last one is the good one

Comment
Add comment · Show 3 · 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 ferbco · Dec 20, 2012 at 06:53 AM 0
Share

If you still have the code from the Door$$anonymous$$anager javascript and the raycast door opening script in PlayerCollisions and it opened and closed the door properly - maybe you commented it out ins$$anonymous$$d of deleting it? - could you copy and paste it here so I could compare it with $$anonymous$$e and look for the error.

avatar image Nubz · Dec 20, 2012 at 05:38 PM 0
Share

I did my scripts in C# and I only have the finished one saved

avatar image Nubz · Dec 21, 2012 at 12:34 PM 0
Share

also looks like you said you made 2 scripts? This could be the problem since it's supposed to be on plus it looks like you are missing parts of it I would redo that chapter if I were you

avatar image
0

Answer by ferbco · Dec 21, 2012 at 09:47 PM

error located. had to change the code a bit from what was shown in the book

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

Answer by willgoldstone · Dec 21, 2012 at 11:39 AM

Hi Ferbco, that's one heck of a headline! Not sure why you're seeing this. Despite the fact that your raycast occurs each frame as DuxDucis points out, the if(!doorIsOpen) flag should stop you doing this more than the first time, until that variable is reset.

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

Answer by Dave-Carlile · Dec 21, 2012 at 12:45 PM

Looking through the scripts it seems like they should work properly. While facing the door it should open, then close 3 seconds later, then repeat.

The doorOpenTime variable is controlling how fast the door opens and shuts. While the value is 3 in the script, if it was ever overidden in the editor then the editor value will take precedence - even if it was 0 when the script was first added to the gameobject, it will remain 0 even if you're changing the value in the script. So verify that the value shows as 3 in the editor. If it doesn't, right click on the component in the editor and select "reset", and it will grab the default values from the script again.

Not saying that is for sure the problem, but I've been bitten by that quite a few times, where I'm changing constants in a script to adjust things, but the editor keeps the values it had until I reset.

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Raycast shooting script 1 Answer

Disabling a lineRender or Ray when there is no target 1 Answer

Weird Glitch When Instantiating a Game Object 1 Answer

racing game raycast problem 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