Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Noddman · Apr 22, 2011 at 01:37 AM · transformtriggerdoorkeyopen

Open door with key

This script is for my door. Right now it opens every time I walk into the invisible cube that triggers the door to open. I made a script to pick up a key. Now the big question is how do I change the script below so it triggers only when I have the key. I think I have to somehow call the other script and let this one know I have the key.

Door open script:

var door: Transform; var angleOpen: int; var angleClose: int; var speedOpen: int =1000;

function OnTriggerStay (other: Collider) { if(door.transform.localEulerAngles.y < angleOpen) { door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen); } }

Below is the pick up script for the key.

private var gotKey : boolean = false;

function OnControllerColliderHit (hit: ControllerColliderHit) {

if(hit.gameObject.name == "key") { print("You Picked Up The House Key"); gotKey = true; Destroy(hit.gameObject);
}
}

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 Antolikaboss50505 · Sep 01, 2014 at 08:00 AM 0
Share

What sort of script does this go on?

2 Replies

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

Answer by loramaru · Apr 22, 2011 at 04:11 AM

You will indeed need to call a function (or access an public variable) on the other object. For the sake of the example, I'll assume the script with the key code is PlayerScript; but just substitute the real name below. The first thing to do is to add a function to get the key state in that script

private var gotKey : boolean = false;

function HasKey() { return gotKey; }

function OnControllerColliderHit (hit: ControllerColliderHit) { // ... }

Then in your door script, you just need to grab the script on the player object using GetComponent(type). Once you have the component, you can call any public function (or access any public variables, from that script.

function OnTriggerStay (other: Collider) {
    var player : PlayerScript = other.gameObject.GetComponent(PlayerScript) as PlayerScript;
    if(player != null && player.HasKey()) {
        if(door.transform.localEulerAngles.y < angleOpen) {
            door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen);
        }
    }
}
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 Noddman · Apr 22, 2011 at 09:16 AM 0
Share

Thanks. With $$anonymous$$or change I made this work.

 PlayerScript = GameObject.Find("Player").GetComponent("PickuUp");    
 if(PlayerScript.got$$anonymous$$ey) {
avatar image
0

Answer by burgunfaust · Apr 22, 2011 at 04:17 AM

I believe this will work.

Key script

var doorKeyOpens : Transform; private var gotKey : boolean = false;

function OnControllerColliderHit (hit: ControllerColliderHit) {

if(hit.gameObject.name == "key") { print("You Picked Up The House Key"); doorKeyOpens.gotKey = true; Destroy(hit.gameObject);
}
}

Door script

var door: Transform; var gotKey : boolean = false; var angleOpen: int; var angleClose: int; var speedOpen: int =1000;

function OnTriggerStay (other: Collider) { if(door.transform.localEulerAngles.y < angleOpen) { door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen); } }

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

1 Person is following this question.

avatar image

Related Questions

Help with making his work with a key 1 Answer

Opening an animated door with a keycard 0 Answers

Press an in-game switch to open a door - help? 2 Answers

Open door with key 1 Answer

GUI text, door and object collider 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