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
2
Question by ebilrubberducki · Mar 18, 2014 at 11:51 AM · basickeyspick up object

Simple pickup key and unlock door

Hi, I'm relatively new at coding in unity and I've been looking for over a week now but I would like to know if anybody has any idea on how to code a simple code that will allow a player to pick up a key object and then once the key has been picked up, unlock a specific door. I've looked at both javascript and c# but I don't seem to be having any success. Any help will be appreciated :D

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Yofurioso · Mar 18, 2014 at 12:53 PM

This tutorial may help: http://www.youtube.com/watch?v=4l2YJsgmusI

It explains how to pick up items, then you just need to have a boolean variable to know if your player is holding the key, and if he is, open the door.

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 Calum-McManus · Mar 18, 2014 at 12:27 PM

This website really isn't for handing out code samples, but I'll give you some tips on where you can start so you can learn to code not just leach of others.

First you will want a RayCast to your mouse position or centre of you screen (depending on how you game works)

Have the RayCast check what it's hitting when you click (Input.GetMouseButtonDown).

Then if the ray cast hit an object with the tag "Key", destroy the key and make a boolean (e.g. private bool GotKey) true, then check if the ray cast is hitting an object with the tag "Door", if it is and "GotKey" is true, Open door.

If you really can't figure it out I can provide you with the script (in C#) but that will not help you learn at all.

Mainly research RayCasts, RayCastHits and If statements, its all you need to make this script.

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 ebilrubberducki · Mar 22, 2014 at 03:53 PM 0
Share

Hey Calum.$$anonymous$$c$$anonymous$$anus I've been trying for a while now but I'm not sure I'm on the right idea as I keep getting complier errors and so I can't see if the code actually works. I have two codes, one to go onto the door that needs to be unlocked and on the would go on the player I'm guessing.

Here's the key script: #pragma strict

 public var $$anonymous$$eyGot : boolean = false;
 var hit : RaycastHit;
 
 
 
 function Update () {
 
     if (Physics.Raycast (transform.position, transform.forward, hit, 1)){
     (Input.Get$$anonymous$$ouseButtonDown && (hit.collider.tag("key")));
      Destroy(GameObject.FindWithTag("key"));
     $$anonymous$$eyGot = true;
      
     } 
 }

And here's the door script: var smooth = 2.0; var DoorOpenAngle = 90.0; var DoorCloseAngle = 0.0; var open : boolean; var enter : boolean;

 function Update (){
 
 if(open == true && $$anonymous$$eyGot == true){
 var target = Quaternion.Euler (0, DoorOpenAngle, 0);
 transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
 }
 
 if(open == false && $$anonymous$$eyGot == false){
 var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
 transform.localRotation = Quaternion.Slerp(transform.localRotation, target1, Time.deltaTime * smooth);
 }
 
 if(enter == true){
 if(Input.Get$$anonymous$$ouseButtonDown(0)){
 open = !open;
 }
 }
 }
 
 function OnTriggerEnter (other : Collider){
 
 if (other.gameObject.tag == "Player") {
 (enter) = true;
 }
 }
 
 function OnTriggerExit (other : Collider){
 
 if (other.gameObject.tag == "Player"){
 (enter) = false;
 }
 }

If you can help it would be much appreciated :D

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

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

Related Questions

How to create a basic follow AI 1 Answer

Script for collectiong keys to escape 2 Answers

looking for help with item pickups 1 Answer

Inventory system? 3 Answers

each enemy have different damage amount, how to calculate the hp remain??? 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