Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • 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.

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

Help with limiting turret rotations 0 Answers

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

GetAxis Horizontal/Vertical not working with unscaledDeltaTime 0 Answers

Get angle of rotation around local axis 1 Answer

Can only go horizontally while walking on walls, and not vertically 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