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");
}
}
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;
}
}
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
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;
}
}
so now set the answer as best and close the subject I enjoy to see it's helpfull
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.
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)
I'm creating a crouch script (so Y axis) so I gonna trasnlate it in X axis
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