hello. i am trying to create lift elevator. i want to do elevator trigger with button on scene. but i do not know how can i do this.
OK, what your asking is not the easiest thing to do but, it isn’t that hard in the end run, you just need some guidance to help you along. I am not going to write your code out, but I will give you the synopsis on how it all works.
Everything that I am writing in here can be found in the reference and on the forums. ![]()
First part would be first, setup a lift, setup a control panel. You will also need to setup a trigger inside the elevator that takes up the interior space of it. This needs to be attached to a single object which will move (call it Elevator). Lastly, for simplicity’s sake, I would further attach that object to a parent object (call it Mount), so when you move, you move in local space, not world space.
Set the Mount object to the base floor, the Elevator object to Vector3.zero (hopefully you wont be screwing with rotation on this, so it will be simple.
Now comes the code part.
You will basically need 2 scripts, one that handles elevator operation, the other is on each button in the outfit.
First, you need a code to open and close the elevator doors. This can be handled in the Elevator’s script, simply give it a right door and left door and make them close or open or however you want it to work. When closed, the elevator can move, if not, they can’t. (you could put a failsafe in the door that says, if something is between them, reopen)
You will then need to tell your trigger that whatever walks into it, is now parented by the Elevator. This is because first and 3rd person controllers don’t like moving platforms, so you have to explicitly say, “Move with me stupid”.
Next, you will need a piece of script in the elevator that accepts button presses. Press X and it adds X to the list (array) if it is not already on it : ButtonPressed(X)
You will then need a piece of script that moves the elevator from level to level starting with the direction first pointed to in the button pressed array. At each floor between then and there it should ask if that floor is on the list, and stops at it if it is. It will then clear that button from the floor list. This represents the tendency for elevators to stop at another floor on your way up or down.
Each button on the elevator can make the elevator move to another floor, so you will need a script that tells the elevator that floor x was pushed and it should go to it. I would simply have it broadcast to the button’s parent object(Elevator) ButtonPressed(x) this would add a floor to be stopped at.
Lastly, you will want a piece of script on the buttons outside the elevator that say, stop at my level and pick me up, I am going up or down. (the up or down part is not completely necessary but for realism you don’t want to get on an up elevator if your going down.
If a particularly anal player would want to press all the buttons to see what happens, the elevator should start towards the first position, then stop at any floor pressed until it reaches the first button pressed. The second button at that time becomes the first, and it goes in that direction until it runs out of buttons. Then it simply waits at that level until someone presses a button.
can you write simple code for me. i am new on unity and javascript
If you want completed, working code, try the ‘Collaboration’ forum. Otherwise, if you’re not sure where to start with coding it yourself, try starting with something a little simpler (the ‘elevator’ functionality you describe, while not terribly complicated, isn’t necessarily ‘beginner material’).
Try starting with pressing the button:
http://unity3d.com/support/documenta…s.Raycast.html
Once you have that working mess around with moving the elevator
Then you’ll need to learn a few other things like Vector3.Lerp (allows your movement of the elevator to appear smooth) and Vector3.Clamp (allows the movement of the elevator to stop and start from specific points)
If all of this is too hard then you need to start at the very beginning:
http://unity3d.com/support/documentation/ScriptReference/index.Common_Operations.html
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://unity3d.com/support/documentation/ScriptReference/index.Vectors.html
hey guys. i am trying to open door. i create box collider and animation clip. i create
function OnTriggerEnter(other: Collider) {
animation.Play (“dooropenclose”);
}
code and put it to box collider. but animationclip not start. i put animtion to box collider also animation start but box collider move as animation. i want to create " insert box collider and animation clip start" script.
The best bet is not to use animation clips, do it programatically using Vector3.Lerp() functions. (look that up in the documentation) So you Lerp from 0 to 1 over the course of .5 seconds and 0 to -1 the other way over the same .5 seconds.
hello BigMisterB. i need your help. i could not do slide door with Vector3.Lerp. can you send me simple example?
http://forum.unity3d.com/threads/52124-slide-open-door-by-using-transform-coordinates?highlight=lerp
Your honestly going to use Lerp alot… so do look at the reference ![]()
i look Lerp example on unity. but i could not understand how can i use it. with collider as trigger
Have you tried working through some tutorials? If not, doing so would probably help you get a better grasp of some of the basics.
i used Vector3 and i created some project. but something wrong. Jesse do you want me to send my whole project?
For now at least, I’d recommend you just stick with your thread and post questions here. You can post any relevant code that you have (remember to use ‘source’ tags). If you’re getting compiler errors or run-time errors, post those; otherwise, just describe what’s going wrong.
it is my open door script
var Player: Transform;
var Door: Transform;
var to: Vector3;
var from:Vector3;
var doorspeed: int=5;
function OnTriggerStay(other: Collider){
if (Door.transform.position.x < from.x){
Player.parent = Door;
Door.transform.Translate(Vector3.right*Time.deltaTime*doorspeed);
}
}
and close door code
var Player: Transform;
var Door: Transform;
var to: Vector3;
var from:Vector3;
var doorspeed: int=5;
function OnTriggerStay(other: Collider){
if (Door.transform.position.x > to.x){
Player.parent = Door;
Door.transform.Translate(-Vector3.right*Time.deltaTime*doorspeed);
}
}
can i create 1 script for this 2 script.
this is project download link http://rapidshare.com/files/440041928/Project.rar
when i enter box collider i am sliding with door in box but box is static.and FPS is deforming when insert box collider. change scale