Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 bailuvivi · Apr 24, 2010 at 08:48 AM · javascriptscriptingbasics

How to make player open two or more different doors?

I am not familiar with script.

In my scene there are two building, and both have a its own door. Then I followed a vedio tutorial and copy Javascript into my project. Below is the script named playercollisions I have modified.

var doorOpened : boolean = false; var timer : float = 0.0;

function OnControllerColliderHit(hit:ControllerColliderHit){

 if((hit.gameObject.tag == "house1door") && (doorOpened == false)){
     openDoor();
 }   

}

function Update(){

 if(doorOpened){
     timer += Time.deltaTime;
 }

 if(timer >= 3){
     shutDoor(); 
 }

}

function shutDoor(){

 var theHouse = gameObject.FindWithTag("house1");
 theHouse.animation.Play("doorshuts");
 doorOpened = false;
 timer = 0;

}

function openDoor(){

     doorOpened = true;
     var theHouse = gameObject.FindWithTag("house1");
     theHouse.animation.Play("dooropen"); 

}

Then I added the script to First Person Controller(Its tag is "Player").And added "house1" tag to the two buildings in the scene, and added "house1door" tag to the two doors of each building. I renamed the same name with the animations of the two models. Opening animation of the two buildings has the same name "dooropen", and shutting animation name is "doorshuts". But when I run the game, player can open doorA and can't open the other doorB. Then I quit Unity and restart, run the game and I found I could open doorB and couldn't open doorA. What can I do to solve the problem? Thanks a lot!

Comment
Add comment · Show 1
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 AVividLight · Jul 06, 2010 at 04:06 PM 0
Share

Is the video tutorial from Learn $$anonymous$$e Silly ( http://www.learnmesilly.com/ )?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Loius · Oct 02, 2010 at 11:02 PM

GameObject.FindWithTag only finds the first object with that tag.

You need to pass the object you hit as an argument. There's little reason to use any Find function outside of a Start or Awake script if it can be helped.

if((hit.gameObject.tag == "house1door") && (doorOpened == false)){ OpenDoor( hit.gameObject ); }

function OpenDoor( go : GameObject ) { go.animation.Play(whatever); }

Also, keeping functions named in CamelCase will help with code readability in the long run. :)

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
1

Answer by TaigaStudios · Jul 06, 2010 at 11:21 AM

I found similar scripting in Unity Game Development essentials book, where you must collect 4 batteries in order to open the door. The script works fine when you duplicate the building and have collected the 4 batteries once.

Could you point out which video tutorial were you following. and try duplicating the working building, if that doesn't work make sure you have requirements to open the door. I believe you have that script attached to player and you only have a tag on the door, just like in the tutorial i followed, in that sense it should work.

In general i recommend for doors that you use a raycast and not a box collider. With raycast the player must look at the door in order to open it. And cant reverse to the building.

If the tutorial comes with link to a complete project, check that you have everything the same, and ask the maker of tutorial can you use this script for multiple buildings, or post a link to the video and i'll have a look.

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Creating & accessing a function from a diffrent "OnTriggerEnter" Function 1 Answer

Does Unity Script syntax change from release to release? If so how much? 4 Answers

Difference between UnityScript and JavaScript? 1 Answer


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