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 /
This question was closed May 07, 2018 at 08:15 AM by Myth for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Myth · Jan 28, 2011 at 04:58 AM · buttondooropenclose

Simple Script - and no result

Can someone tell me what I have done wrong? This is a simple script that should open and close a double door after pushing a button on a wall.

When I test it I get "b" in the debug but not "a" - so the doors close but don't open again (also note the doors were closed to start with)

var door_left : Transform;

var door_right : Transform;

var door_left_startX : float; var door_right_startX : float;

var countA : float = 0; var door : boolean = true;

function Update () { var hit : RaycastHit;

//find forward then test for x meters in front of position if (Physics.Raycast (transform.position, transform.forward, hit, 2)) { if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) {

     if (door == true && countA == 0)
     {
         countA = -1;
         door = false;
         print ( "a");
     }

     if (door == false && countA == 0)
     {
         countA = 1;
         door = true;
         print ( "b");
     }   
 }

}

if (countA > 0) { countA = countA - 0.01; }

if (countA < 0) { countA = countA + 0.01; }

door_left.transform.position = Vector3(door_left_startX+(countA*-1), door_left.transform.position.y, door_left.transform.position.z); door_right.transform.position = Vector3(door_right_startX+countA, door_right.transform.position.y, door_right.transform.position.z);

}

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

  • Sort: 
avatar image
0
Best Answer

Answer by Myth · Jan 29, 2011 at 01:49 AM

Finally it works! - wouldn't have figured it out if you hadn't told me about the Boolean thingy though.

Thanks for your help!

Here is the final code:

var door_left : Transform;

var door_right : Transform;

var door_left_startX : float; var door_right_startX : float;

var countA : float = 0; var opening : int = 0;

function Update () { var hit : RaycastHit;

//find forward then test for x meters in front of position if (Physics.Raycast (transform.position, transform.forward, hit, 2)) { if(hit.collider.gameObject.tag=="Player" && Input.GetAxis ("e_key") ) { if (countA <= 0) { opening = -1; } if (countA >= 1) { opening = 1; } } }

if (opening == 1 && countA > 0) { countA = countA - 0.01; }

if (opening == -1 && countA < 1) { countA = countA + 0.01; }

door_left.transform.position = Vector3(door_left_startX+(countA*-1), door_left.transform.position.y, door_left.transform.position.z); door_right.transform.position = Vector3(door_right_startX+countA, door_right.transform.position.y, door_right.transform.position.z);

}

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 3DMagicVR · Jan 31, 2011 at 06:59 PM 0
Share

Hi, you are right, I made a mistake with the parameters on the door position stuff, now I made a correction and the function work fine, I got two doors working with just one value that open when the character it's close enough and close the doors whe the character pass through the doors.

avatar image
2

Answer by 3DMagicVR · Jan 28, 2011 at 06:16 AM

Hi, try to use this:

function Update() { var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.forward, hit, 2)) { if(hit.collider.gameObject.tag == "Player" && /* The button action is activated */) { if (door == true) { // Opend the doors countA += 0.01; } } }

 if (countA == 1) { // The doors are totally open
     door = false;
 }

 if (countA &gt; 0 &amp;&amp; door == false) { // Close the doors
     countA -= 0.01;
 }
 door_Left.localPosition = Vector3(-countA, 0, 0);
 door_Right.localPosition = Vector3(countA, 0, 0);

}

This should work, when work with two objects (one in front another) just type value and -value to set positive and negative motion, the recommendation it's to use always positive number so this can be made negative using the "-" before the value, if you use the boolean state in a "if" statement with the same boolean as a condition this will interrupt if condition and the loop will be interrupted.

Hopes this help.

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 Myth · Jan 29, 2011 at 01:00 AM 0
Share

Sorry, I have been unable to get this to produce the desired result. With your code the doors close and end up on top of each other and then as with my code I am unable to "Open" the doors again.

Thanks for attempting to help

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why is my door opening and closing at the same time????????? 2 Answers

Closing sliding door Raycasting 1 Answer

Trying to open door but not close the door aswell!!! 2 Answers

Door opening with a key, my scripts and problem 1 Answer

Openable Door. Please help me with this!! 0 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