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 Ediir · Sep 23, 2017 at 06:09 AM · transformtime.deltatimetransform.translate

Trying to make sliding doors. With transform, but it jumps to position. What am I doing wrong?

Hello.

Like my subject says. I am no coder, still learning. Trying to make sliding doors, but at the moment, it just jumps between the cordinates i have set. I want it to move smoothly to position, can anyone give me a hand with this? I have tried to read documentation, and searched around on google, without any luck.

Thanks in advance. (Let me know if you need any more info);

 public class SliderDoor : MonoBehaviour {
 
     [Header("Speed and new position")]
     public float SlideSpeed = 1.0f;
     public float moveSpeed; // FOR TESTING, CHANGE TO PRIVATE - Value changes from 0.0.007408588 to 0.0165667 - is this right?
     public Vector3 MoveTo;
 
     [Space(15)]
     [Header("GUI Text")]
     public string OpenText = "Click E to open door";
     public string CloseText = "Click E to close door";
 
     // PRIVATE VARIABLES
     // VECTOR 3
     private Vector3 OldPosition;
     // BOOL
     private bool _isInsideTrigger = false;
     private bool isDoorOpen = false;
     
 
     void Start()
     {
         OldPosition = transform.localPosition;
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             _isInsideTrigger = true;
             Debug.Log("PLAYER IS INSIDE TRIGGER AREA - _isInsideTrigger = " + _isInsideTrigger);
 
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             _isInsideTrigger = false;
             Debug.Log("PLAYER EXITED TRIGGER AREA - _isInsideTrigger = " + _isInsideTrigger);
 
         }
     }
 
 
     void Update()
     {
         moveSpeed = SlideSpeed * Time.deltaTime; // CAN I SET THIS IN START? - Test when door is sliding
 
         if (!isDoorOpen && _isInsideTrigger)
         {
     
             if (Input.GetKeyDown(KeyCode.E))
             {
                 SlideDoorOpen();
           }
         }
         else if (isDoorOpen && _isInsideTrigger)
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 SlideDoorClose();
             }
         }
     }
 
     void SlideDoorOpen()
     {
         isDoorOpen = true;
         transform.localPosition = Vector3.MoveTowards(OldPosition, MoveTo, moveSpeed);
     }
 
     void SlideDoorClose()
     {
         isDoorOpen = false;
         transform.localPosition = Vector3.MoveTowards(MoveTo, OldPosition, moveSpeed);
     }
 
     void OnGUI()
     {
         if (_isInsideTrigger && !isDoorOpen)
         {
             GUI.BeginGroup(new Rect(Screen.width / 2 - 200, 100, 450, 420));
             GUI.Box(new Rect(0, 0, 330, 80), "");
 
             GUI.Label(new Rect(15, 30, 400, 23), OpenText);
             GUI.EndGroup();
         }
 
         else if (_isInsideTrigger && isDoorOpen)
         {
             GUI.BeginGroup(new Rect(Screen.width / 2 - 200, 100, 450, 420));
             GUI.Box(new Rect(0, 0, 330, 80), "");
 
             GUI.Label(new Rect(15, 30, 400, 23), CloseText);
             GUI.EndGroup();
         }
     }
 }
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

1 Reply

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

Answer by Positive7 · Sep 23, 2017 at 03:38 PM

  private void Update()
     {
         if (Input.GetKeyDown(KeyCode.E) && _isInsideTrigger)
         {
             isDoorOpen = !isDoorOpen;
             moveSpeed = 0f;
         }
 
         if (isDoorOpen && _isInsideTrigger)
         {
             transform.position = Vector3.MoveTowards(transform.localPosition, MoveTo, moveSpeed);
             moveSpeed += Time.deltaTime / SlideSpeed;
         }
         else
         {
             transform.position = Vector3.MoveTowards(transform.localPosition, OldPosition, moveSpeed);
             moveSpeed += Time.deltaTime / SlideSpeed;
         }
     }
Comment
Add comment · Show 3 · 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 Ediir · Sep 23, 2017 at 06:22 PM 0
Share

Thank you for your answer. Tried to copy paste the code in, it moves smoothly, but it dosnt stop at "$$anonymous$$oveTo" cordinates, and starts moving once i enter play mode. Dont get the chance to get inside trigger, and click "E".

avatar image Positive7 Ediir · Sep 23, 2017 at 07:13 PM 0
Share

Updated the answer! maybe a second check is needed to see if transform.localPosition equals with $$anonymous$$oveTo

 if (isDoorOpen && _isInsideTrigger)
 {
     if (transform.localPosition != $$anonymous$$oveTo)
     {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.localPosition, $$anonymous$$oveTo, moveSpeed);
         moveSpeed += Time.deltaTime / SlideSpeed;
     }
 }
 else if (!isDoorOpen)
 {
     if (transform.localPosition != OldPosition)
     {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.localPosition, OldPosition, moveSpeed);
         moveSpeed += Time.deltaTime / SlideSpeed;
     }
 }



avatar image Ediir · Sep 23, 2017 at 07:37 PM 0
Share

Perfect. Thank you very much :)

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

133 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 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 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 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 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 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 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

C# I need to move object in a cycle 0 Answers

Limit movement to just forward/backward and left/right 0 Answers

Limit movement to just forward/backward and left/right 0 Answers

How do I stop transform.Translate using time? 2 Answers

transform.Translate() effect is being reverted 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