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 robvdbogaert · Nov 07, 2015 at 08:15 PM · javascriptvector3easing

Noob question: easing slidingdoor animation

I'm completely new to unity, been working in it for 10 weeks now. I want to make a sliding door, but I don't know how. I've got a script that makes it move instantly, is it possible to ad an easing function in this script, or do I have to rescript it some other way?

 var enter : boolean; 
 var open : boolean;
 
     function Start (){
         
     }
     
     
     function Update ( ){
         var deur = GameObject.FindWithTag("BodySchuifdeur");
         
      if(open == true){ 
         deur.transform.localPosition = Vector3(-3.3, -0.65, 0);
       }
      
     if(open == false){
         deur.transform.localPosition = Vector3(0, -0.65, 0);
       }
      
        if(enter == true){
          if(Input.GetKeyDown("f")){
       open = !open;
          }
       }
 
     }
 
 
     //Activate the Main function when player is near the door
      function OnTriggerEnter (other : Collider){
      
       if (other.gameObject.tag == "Player") {
       (enter) = true;
       }
      }
 
      //Deactivate the Main function when player is go away from door
      function OnTriggerExit (other : Collider){
      
       if (other.gameObject.tag == "Player") {
       (enter) = false;
       }
      }
 
 function OnGUI(){
         if(enter){
         GUI.Label(new Rect(Screen.width/2 - 80, Screen.height - 100, 250, 30), "PRESS F to open/close the door");
         }
      }
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 Vylax · Nov 08, 2015 at 12:11 PM 0
Share

First you must create an empty gameObject, (you can call it charner) and put it exactly we the charner must be on X and Z axe (we are killing Y axe) if you don't really understand look this : https://www.youtube.com/watch?v=vfL7kQeZtic

Add this script to the door. Smooth is the speed of the door open DoorOpenAngle is if you have a regular door (90°) you can change these two variable If you don't want to have the text say : "press [e] to open the door" remove the OnGUI function, BUT YOU $$anonymous$$UST TAG YOUR PLAYER AS "Player" TAG, Capslock to be sure you read this.

 var smooth = 2.0;
 var DoorOpenAngle = 90.0;
 private var open : boolean;
 private var enter : boolean;
 
 private var defaultRot : Vector3;
 private var openRot : Vector3;
 
 function Start(){
 defaultRot = transform.eulerAngles;
 openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
 }
 
 //$$anonymous$$ain function
 function Update (){
 if(open){
 //Open door
 transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
 }else{
 //Close door
 transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
 }
 
 if(Input.Get$$anonymous$$eyDown("e") && enter){
 open = !open;
 }
 }
 
 function OnGUI(){
 if(enter){
 GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press [E] to open the door");
 }
 }
 
 //Activate the $$anonymous$$ain function when player is near the door
 function OnTriggerEnter (other : Collider){
 if (other.gameObject.tag == "Player") {
 enter = true;
 }
 }
 
 //Deactivate the $$anonymous$$ain function when player is go away from door
 function OnTriggerExit (other : Collider){
 if (other.gameObject.tag == "Player") {
 enter = false;
 }
 }


2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Vylax · Nov 11, 2015 at 06:22 AM

https://youtu.be/rOQrVyeeUI8 this is a video were I explain what to do with my script and here's the script : http://pastebin.com/RWqPxZ5T

Comment
Add comment · Show 2 · 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 robvdbogaert · Nov 16, 2015 at 06:22 PM 0
Share

It was perfect! I just needed to change the X axis to Z axis but I had enough understanding of the code to change that myself. I also used the Body's transform setting. This is my eventual code:

 /////////////////////////////////////////////////////////////////////////////////////////
 //            Vylaxez : https://www.youtube.com/channel/UC9$$anonymous$$3-N-xrQ11jn3psuVd3Cg           //
 //            S$$anonymous$$m : http://s$$anonymous$$mcommunity.com/id/Vylaxez/                               //
 //            Unity3d : ThePixelGamers                                                   //
 /////////////////////////////////////////////////////////////////////////////////////////
 
 private var enter : boolean; //this is enabled when the player is ontriggerentenr from the door
  
  var DoorLeftSide : Transform; //This is the left side of your door
  
  var DLSOpenPositionZ : float; //this is were the left side of your door must be we the door is open
  var DLSClosePositionZ : float; //this is were the left side of your door must be we the door is close
  
  var DLSYPosition : float; //this is the Y position of your door
  var DLSXPosition : float; //this is the Z position of your door
  
  var smoothTime : float = 0.3; //Change this change the time it take to open the door
 private var targetZ : float = 0; //DON'T TOUCH THIS !
 private var velocityZ : float = 0; //DON'T TOUCH THIS TO !
 
 var ShowGUILabel : boolean = true; //Put it on if you want to read "press f to open the door" when you are near the door
 
 private var open : boolean = false;
  
  
 
  function Update (){
  
         var newZ : float = $$anonymous$$athf.SmoothDamp(DoorLeftSide.transform.localPosition.z, targetZ, velocityZ, smoothTime);
  
           if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F) && enter == true)
          {
              open = !open; //reverse open : if open is true, open become false and if open is false, open become true
          }
  
          if(open)
          {
               targetZ = DLSOpenPositionZ;
               DoorLeftSide.transform.localPosition = new Vector3(DLSXPosition, DLSYPosition, newZ);
          }else{
              targetZ = DLSClosePositionZ;
              DoorLeftSide.transform.localPosition = new Vector3(DLSXPosition, DLSYPosition, newZ);
          }
  }
  
  function OnGUI () {
  
      if(ShowGUILabel == true && enter == true)
      {
          GUI.Label(Rect(Screen.width / 2 - 100, Screen.height / 2 - 250, 200, 20), "Press [F] to open the door");
      }
  
  }
  
  
  //Activate the $$anonymous$$ain function when player is near the door
  function OnTriggerEnter (other : Collider){
  if (other.gameObject.tag == "Player") {
  enter = true;
  }
  }
  
  //Deactivate the $$anonymous$$ain function when player is go away from door
  function OnTriggerExit (other : Collider){
  if (other.gameObject.tag == "Player") {
  enter = false;
  }
  }
avatar image Vylax · Nov 25, 2015 at 08:19 AM 0
Share

so now set the answer as best and close the subject I enjoy to see it's helpfull

avatar image
0

Answer by robvdbogaert · Nov 09, 2015 at 10:46 AM

@ThePixelGamers - I'm using that script you posted for a normal openable door (with rotation), I'm looking for a sliding door, that just slides to the side. Thanks for the idea but that's not what I'm looking for. The door just needs to slide along the X-axis smoothly when activated with the F key.

Most preferably I'd like to make this happen: when you approach, the GUI indicator to press F is activated, when pressed the door slides open smoothly and automatically slides back after like 10 seconds. If that's too much trouble, then I need just the easing part, make it animate instead of instantly moving to the new position which happens in my current script.

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 Vylax · Nov 09, 2015 at 06:39 PM 0
Share

Ok, I gona make you a sliding (on X axis) door. You will at least got it this week-end (sorry for my bad english)

avatar image Vylax · Nov 09, 2015 at 06:42 PM 0
Share

I'm creating a crouch script (so Y axis) so I gonna trasnlate it in X axis

avatar image Vylax · Nov 10, 2015 at 11:07 PM 0
Share

https://youtu.be/rOQrVyeeUI8 this is a video were I explain what to do with my script and here's the script : http://pastebin.com/RWqPxZ5T

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

37 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

Related Questions

Get Vector3s in range 1 Answer

How to disable diagonal Vector3 movement of non-player object using MoveTowards? 1 Answer

Bacteria/Cell Growth 1 Answer

If you collide with an object destroy the object updated for unity 5 ? 3 Answers

Speed Increase Power up 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