Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Double-V · Aug 25, 2016 at 08:23 PM · vector3translatemovetowards

[Solved] Sliding garage door using transform.translate.

I'm trying to make a door that works like this:

  • when a player enters trigger zone he becomes capable of initiating the opening/closing of said door with a button

  • the door stops moving when it reaches a point 2 units higher in the y axis than it's original position (if it's opening)

  • the door stops moving when it reaches it's original position if it's closing

  • the player can switch directions of the door moving mid-move

So i made those 2 scripts and both don't work (i'm excluding some parts for clarity eg. the trigger events that set player_inrange variable,):

 private bool player_inrange = false;
     private Transform state_closed;
     private Transform state_open; //transform that door has to have after it's opened
     private bool wanted_open = false;
     public float speed;
 
     void Start()
     {
         state_closed = gameObject.transform;
         state_open.position = state_closed.position + new Vector3(0f, 1f, 0f);
     }
 
  void Update()
     {
         if (Input.GetButtonDown("Use") && player_inrange == true) //if player is in range and presses e(default)
         {
             wanted_open = !wanted_open;
             Debug.Log("You want to switch the door"); //i get this one
         }
         if (wanted_open == true) //if they want to be open
         {
             transform.position = Vector3.MoveTowards(transform.position, state_open.position, Time.deltaTime * speed);
             Debug.Log("the door should be opening"); //but not this one
         }
         if (wanted_open == false) //if they want to be closed
         {
             transform.position = Vector3.MoveTowards(transform.position, state_closed.position, Time.deltaTime * speed);
         }
     }

I don't really know why this isn't working. I get the first debug log, but not the second.

Now, this is the different script i've tried. This one uses regular transform.translate, but should stop the moment it reaches the thing specified in if statements, but... it doesn't, instead it stops in random places - the higher I make the speed variable the faster it goes, but stops further and further away than it should.

 void Start()
     {
         current_transform = gameObject.transform;
         state_closed = gameObject.transform;
         state_open.position = state_closed.position + new Vector3(0f, 1f, 0f);
 }
 
 void Update()
     {
         current_transform = gameObject.transform;
         if (Input.GetButtonDown("Use") && player_inrange == true) //if player is in range and presses e(default)
         {
             wanted_open = !wanted_open;
             Debug.Log("You want to switch the door");
         }
         if (wanted_open == true && current_transform != state_open) //if they want to be open, but aren't (it should stop when it reaches state_open)
         {
             gameObject.transform.Translate(Vector3.up * Time.deltaTime * speed);
             Debug.Log("the door should be opening");
         }
         if (wanted_open == false && current_transform != state_closed) //if they want to be closed, but aren't (it should stop when it reaches state_closed)
         {
             gameObject.transform.Translate(Vector3.down * Time.deltaTime * speed);
         }
     }

So it's weird and doesn't work and at this point every other question that asks for sliding doors or "how to move an object over time" seems like gibberish to me, becasue i can't find what's wrong.

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 ScaniX · Aug 25, 2016 at 08:27 PM 1
Share

In the first example there really isn't anything other than an error (which you would see in the log) stopping him from printing out the second debug log whenever player_inrange is true and you press that button.

But as a start I would change your variables. You start position is referencing the gameObject.transform, so changing the current transform of your door will change the state_closed position at the same time. Use Vector3 for your positions.

1 Reply

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

Answer by Double-V · Aug 26, 2016 at 10:20 AM

So I changed the script in some ways, the most notable thing is that the if statements check for a float that is the value of the y axis, because I don't really need the other axes.

 using UnityEngine;
 using System.Collections;
 
 public class Door : MonoBehaviour {
 
     //use this script for doors that require pressing the use button
     //set up the use button in edit >> project settings >> input
     //the door has to have a trigger
 
     private bool player_inrange = false; //is player in range of the door
     private float state_closed; //the value of y axis position when the door is closed
     private float state_open; //the value of y axis position when the door is open
     private bool wanted_open = false; //if the door wants to be open or closed
     private float current_y; //y axis position of the door, updated every frame
     public float speed; //how fast the door moves
     
 
     void Start()
     {
         state_closed = gameObject.transform.position.y;
         state_open = state_closed + 3f;
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.name == "Player")
         {
             player_inrange = true;
             CancelInvoke("IdleCloseDoor");
             Debug.Log("You entered the trigger");
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.name == "Player")
         {
             player_inrange = false;
             Debug.Log("You left the trigger");
             Invoke("IdleCloseDoor", 5f);
         }
     }
 
 
 
     void Update()
     {
         current_y = gameObject.transform.position.y;
         if (Input.GetButtonDown("Use") && player_inrange == true) //if player is in range and presses e(default)
         {
             wanted_open = !wanted_open;
             CancelInvoke("IdleCloseDoor");
             Debug.Log("You want to switch the door");
         }
 
         //if they want to be open, but aren't (it should stop when it reaches state_open)
         if (wanted_open == true && current_y < state_open)
         {
             gameObject.transform.Translate(Vector3.up * Time.deltaTime * speed);
         }
 
         //if they want to be closed, but aren't (it should stop when it reaches state_closed)
         if (wanted_open == false && current_y > state_closed) 
         {
             gameObject.transform.Translate(Vector3.down * Time.deltaTime * speed);
         }
     }
 
 
     //and this makes the door want to close when the player wanders away and the door stays open for too long
         void IdleCloseDoor()
     {
         wanted_open = false;
     }
 }
 

Now it works as intended. I'll change the title of this question, so more people searching for something like this will find it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how can I "play" the whole MoveTowards sequence after one click? 1 Answer

Y value in transform.Translate when not grounded - gravity problem 0 Answers

How can I make this translation smoother? 0 Answers

Vector3.MoveTowards moving transform 0 Answers

How to make individual pressure plates translate up when stepped on, within a parent? 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