• 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 KINDOK · Jun 07, 2016 at 12:05 PM · axislimittransform.translatexdrawer

Set limit to transform.translate

I'm making a drawer that on press "e" open and close if the player is in a trigger. The problem come when I open the drawer, it move in x position and don't stop. How I can set a limit to open? And how I can do that the drawer return to his original position on close? Thanks

 using UnityEngine;
 using System.Collections;
 
 public class Drawer : MonoBehaviour {
 
     public float Smooth = 2.0f;
     //public float MaxDistance = 2.0f;
     public bool Enter;
     public bool IsOpen;
 
     void Start ()
     {
 
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             Enter = true;
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             Enter = false;
         }
     }
 
     void Update ()
     {
         if(IsOpen == true)
         {
             transform.Translate(0, Time.deltaTime, 0);
         }
 
         if(Enter == true)
         {
             if(Input.GetKeyDown("e"))
             {
                 IsOpen = !IsOpen;
             }
         }
     }
 }
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 Superhirn3 · Jun 07, 2016 at 02:34 PM

Well your void Update() is a little bit wrong ;)

  void Update ()
  {
      if(IsOpen == true && MaxDistance < transform.position.y)
      {
          transform.Translate(0, Time.deltaTime, 0);
      }
 
      if(Enter == true)
      {
          if(Input.GetKeyDown("e"))
          {
              IsOpen = !IsOpen;
          }
      }
  }

but if you close the drawer, you need to know, where it was at the beginning. Is the starting position of the drawer (0,0,0)?

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 KINDOK · Jun 07, 2016 at 03:50 PM 0
Share

@Superhirn3 The starting position of the drawer is not (0, 0, 0) it can be placed in the map with a random position, so it can't be a number set in the inspector.

I'm thinking to use animations of open/close, but the drawers will have small physics objects inside. I dont know if it can be physics errors by using a animation so I want to use a script.

How could I get the starting position?

avatar image
0

Answer by Hellium · Jun 07, 2016 at 02:17 PM

For your problem, I advise you another solution : Coroutines

In the inspector, just indicate the closed and opened position of your drawer

 using UnityEngine;
 using System.Collections;
 
 public class Drawer : MonoBehaviour {
 
 public Vector3 OpenPosition ;
 public Vector3 ClosePosition ;
 
 private bool canTrigger;
 private bool isOpen ;
 
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         canTrigger = true;
     }
 }
 
 void OnTriggerExit(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         canTrigger = false;
     }
 }
 
 void Update ()
 {
     if(canTrigger && Input.GetKeyDown("e"))
     {
         isOpen = !isOpen;
         StopAllCoroutines();
         StartCoroutine( slideTo( isOpen ? OpenPosition : ClosePosition) ) ;
     }
 }
 
 private IEnumerator slideTo( Vector3 endPosition )
 {
     while( Vector3.Distance( endPosition, transform.position ) > 0.1f )
     {
         transform.position = Vector3.Lerp( transform.position, endPosition, Time.deltaTime );
         yield return null;
     }
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.LookAt and move towards an object on only two axises. 1 Answer

GetAxis Horizontal/Vertical not working with unscaledDeltaTime 0 Answers

Help with limiting turret rotations 0 Answers

Rotate an object respectively to other object's rotation on z-axis 2 Answers

how do i reference two input axis at one time on javascript 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges