- Home /
Elevator Script ?
how to make an elevator script that transform from target A to target B slowly but you need to activate the elevator by pressing [E] and then the elevator will going down and stop (when arrived at target B or target A). does anyone know how ?
You can look through these two answers and see if they help you out.
ControllerCollider Falls Through or....
Ride an elevator?
Answer by Statement · Dec 12, 2011 at 05:22 PM
I guess by pressing E, you mean pressing E while looking at the elevator?
First, you should check if E is being pressed. If it is, make a raycast to interact with the elevator object. Upon interaction, translate the elevator toward one of the two transforms that indicate the two different floors.
If you'd want to puzzle in @OrangeLightning 's animation solution, you'd replace the translation with the animation ins$$anonymous$$d.
Answer by FLASHDENMARK · Dec 12, 2011 at 05:35 PM
The best(and easiest) way would be to animate the elevator, either using Unity's animation panel or animate it using your favorite 3d app. Now assuming you have animated the elevator, lets get scripting.
var isAtOriginalPosition = true; //Variable to control if the elevator is "up" or "down"
function Update(){
if(Input.GetKeyDown("e")){
ActiveElevator();
}
}
function ActiveElevator(){
if(isAtOriginalPosition){
animation["animationName"].time = 0;
animation["animationName"].speed = 1;
animation.Play("animationName");
isAtOriginalPosition = false;
}
else{
animation["animationName"].time = animation["animationName"].length;
animation["animationName"].speed = -1;
animation.Play("animationName");
isAtOriginalPosition = true;
}
}
This is a script controlling where the position of the elevator is. It is very basic and works. HOWEVER this script is lacking something, you will properly notice the limitations of the code I enclosed(the code is not intended to cover every aspect or problem, etc. but merely a demonstration.).
I will strongly recommend animating the object, and use scripts to activate it. However if you want to use code only you could use something like Mathf.Lerp to 'Lerp' the position to where you want it.
EDIT: If you want to make better use of code without using animations listen to Statement.
Animations are almost always to be preferred, since they can add much more motion details and are easier to tweak. The exception would be a quick-and-dirty solution for a small project or settling with the rather static effect that Vector3.$$anonymous$$oveTowards or Vector3.SmoothDamp can offer. +1
Answer by josprachi · Feb 27, 2019 at 11:41 AM
A ready example might help [elivator example][1]https://crackpracgameshowcase.blogspot.com/p/creating-a.html
Your answer
Follow this Question
Related Questions
Dont transform.translate if it will collide with another object 1 Answer
How can I instantiate and move objects smoothly? 1 Answer
transform.Translate not translating in relation to rotation 0 Answers
Simultaneously translating and rotating 2D sprite? 1 Answer
How to wait until player move to a specific position 0 Answers