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 cycoclash25 · Apr 25, 2012 at 04:44 PM · doorkeyopenwith

Opening a door with a key

I am having some trouble getting my character to open the door once the key is picked up. I used to have it working without the key, but now that I've added the key it won't open the door when the character picks up the key, any suggestions? Heres the code:

Player:


    var doorScript : Door;
    var have_key = false;
    function Update () {
       var hit : RaycastHit;
       Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
       if (Physics.Raycast(transform.position, transform.forward, hit, 2)) {
          if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) ) {
             if (doorScript.doorState == DoorStates.locked) {
                doorScript.doorState = DoorStates.unlocked; 
             } else if (doorScript.doorState == DoorStates.unlocked && have_key) {
                doorScript.Open();
             }
          }
       }
    }
    function OnCollisionEnter (theCollision : Collision){
       if (theCollision.gameObject.tag == "Key")
          have_key=true;
    }

Door Trigger:


    var doorScript : Door;
    function OnTriggerEnter (col : Collider) {
       if ((col.gameObject.tag == "MainCamera") && (doorScript.doorState == DoorStates.unlocked) ) {
          doorScript.Open();
       }
    }

Door:


    enum DoorStates {open, unlocked, locked};
    var doorState : DoorStates;
    function Awake() {
       doorState = DoorStates.locked;
    }
    function Update () {
       Debug.Log(doorState);
    }
    function Open() {
       animation.Play("Open");
       doorState = DoorStates.open;
    }

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 cycoclash25 · Apr 25, 2012 at 04:46 PM 0
Share

also this is the pick up script for the key (used from lerpz): enum PickupType { Health = 0, FuelCell = 1 } var pickupType = PickupType.FuelCell; var amount = 1; var sound : AudioClip; var soundVolume : float = 2.0;

private var used = false; private var mover : Droppable$$anonymous$$over;

function Start () { // do we exist in the level or are we instantiated by an enemy dying? mover = GetComponent(Droppable$$anonymous$$over); }

function ApplyPickup (playerStatus : ThirdPersonStatus) { // A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial. switch (pickupType) { case PickupType.Health: playerStatus.AddHealth(amount); break;

     case PickupType.FuelCell:
         playerStatus.FoundItem(amount);
         break;
 }
 
 return true;

}

function OnTriggerEnter (col : Collider) { if(mover && mover.enabled) return; var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);

 //* $$anonymous$$ake sure we are running into a player
 //* prevent picking up the trigger twice, because destruction
 //  might be delayed until the animation has finished
 if (used || playerStatus == null)
     return;
 
 if (!ApplyPickup (playerStatus))
     return;

 used = true;
 
 // Play sound
 if (sound)
     AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
     
 
 
 // If there is an animation attached.
 // Play it.
 if (animation && animation.clip)
 {
     animation.Play();
     Destroy(gameObject, animation.clip.length);
 }
 else
 {
     Destroy(gameObject);
 }

}

// Auto setup the pickup function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }

@script RequireComponent(SphereCollider) @script AddComponent$$anonymous$$enu("Third Person Props/Pickup")

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Opening a door with a key 2 Answers

Open door with key 2 Answers

Activate object with key 1 Answer

Opening Door with Key 1 Answer

Door opening with a key, my scripts and problem 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