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 Forcyghter · Jan 23, 2014 at 06:11 AM · raycastopendrawer

Open and close a drawer with raycast

Hi, I'm currently using this script, but it don't work. :\ I don't know why, please help me :'(

 var GuiText : GUIText;
 var hit: RaycastHit;
 var canOpenClose : boolean;
 var drawerObject : GameObject;
 var drawerPos1 : Transform;
 var drawerPos2 : Transform;
 var movePosition : Transform;
 
 
 var smoothTime : float = 0.1;
 private var velocity = Vector3.zero;
 var maxSpeed = 8; 
    
 function Start () {
 
  movePosition = drawerObject.transform;
  
 }
  
 function Update () {
 
 //open drawer text
 
 drawerObject.transform.position = Vector3.SmoothDamp(drawerObject.transform.position, movePosition.position,velocity, smoothTime, maxSpeed);
 
 if(Physics.Raycast(transform.position,transform.forward,hit,5)){
  
     if(hit.collider.gameObject.tag=="drawer"){ //add collider reference otherwise you can't access gameObject!
  
        GuiText.text = "Press E to open";
        
        }
     }
     
 else { //text clear
 
 GuiText.text = "";
  
 }    
 
 //open drawer
 
 if(Input.GetKeyDown("e") && canOpenClose){
 
 if(Physics.Raycast(transform.position,transform.forward,hit,5)){
 
 if(hit.collider.gameObject.tag=="drawer"){
 
 canOpenClose=true;
 
         if(Vector3.Distance(drawerObject.transform.position, drawerPos1.position)<0.2){
             movePosition=drawerPos2;
             
         }else{
             movePosition=drawerPos1;
             canOpenClose=false;
         }
         
     }
 }
 }
 
 }
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 joelfivat · Jan 23, 2014 at 12:53 PM 0
Share

Hello,

To which object is this script attached ?

Your player ? Your camera ?

What is not working exactly ?

You locate the problem using Debug.Log("..."); in random places in your code.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Forcyghter · Jan 23, 2014 at 01:13 PM

I attached the script to the main camera (FPS camera). The drawer doesn't move, when the "press E to open" text appears, I click E but the drawer doesn't move.

mmm if I put Debug.LogError or other in random places of the code it tell me I have to fix errors before enter in the play mode. :\

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 joelfivat · Jan 23, 2014 at 03:13 PM

It seems your code is more complex than it needs to be.

Also you should have a separate script for the drawers to keep the drawers state and update the drawer position.

Create a new script Drawer.js with this code, and attach it to all your drawer objects

 public class Drawer : MonoBehavior{
 
     var smoothTime : float = 0.1;
     var maxSpeed = 8;
 
     var origin : Vector3;
     var opening : bool = false;
     
     var velocity = Vector3.zero;
     
     function Start (){
         origin = transform.position;
     }
     
     function Update (){
 
         if(opening){
             drawerObject.transform.position = Vector3.SmoothDamp(origin,
                                                                  origin + transform.forward * 0.2, //open drawer 20cm
                                                                  velocity, smoothTime, maxSpeed);
         }
     }
 }

Then update your camera script :

 var GuiText : GUIText;
 
 function Start () {
 
 }
      
 function Update () {
      
      var hit: RaycastHit;
 
     //open drawer text
     if(Physics.Raycast(transform.position,transform.forward,hit,5)){
      
         GameObject hitObject = hit.collider.gameObject;
         
         if(hitObject.tag=="drawer"){ //add collider reference otherwise you can't access gameObject!
 
             GuiText.text = "Press E to open";
             
             //open drawer
             if(Input.GetKeyDown("e")){
             
                 var Drawer = hitObject.GetComponent(Drawer);
                 Drawer.opening = true;
             }
 
         }
     }
     else { //text clear
 
         GuiText.text = "";
     } 
 }

I have not tested the code, and the syntax may be incorrect as I usually code in C#, but it should work.

Comment
Add comment · Show 8 · 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 joelfivat · Jan 23, 2014 at 03:56 PM 0
Share

One small mistake I made, "Press E to open" will still be displayed when the drawer is open, here is how to fix it :

             if(hitObject.tag=="drawer"){
     
                 var Drawer = hitObject.GetComponent(Drawer);
                 
                 if(!Drawer.opening){
                     GuiText.text = "Press E to open";
                     
                     //open drawer
                     if(Input.Get$$anonymous$$eyDown("e")){
                         Drawer.opening = true;
                     }
                     
                 } else {
                     GuiText.text = "";
                 }
             }
avatar image Forcyghter · Jan 23, 2014 at 07:53 PM 0
Share

It gives me these errors alt text

errore.png (32.0 kB)
avatar image joelfivat · Jan 23, 2014 at 09:53 PM 0
Share

Yes, I am really not used to javascript :P

avatar image joelfivat · Jan 23, 2014 at 09:54 PM 0
Share

I made a quick Unity scene to test the scripts, here is a working example (I hope) :)

https://dl.dropboxusercontent.com/u/4422249/Unity/DrawerTest.unitypackage

Tell me if it works !

avatar image Forcyghter · Jan 24, 2014 at 12:19 AM 0
Share

Thanks, it work...but, when I use this script in my scene (on my drawer) it moves on the Y axis :'( I don't understand why

Show more comments

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

20 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

Related Questions

How to open a drawer with right mouseclick? 0 Answers

Ray cast affects all colliders in scene if mouse button is not releasesd. 1 Answer

Layer Mask Not working to open the door 1 Answer

How to use Raycast to open a crate? 2 Answers

Negative Positions Breaks Raycasts 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