- Home /
Elevator Script.. Please Help!
Hello Guys, I'm currently working on a game project with a few friends of mine and before I start putting the level together I would like to get basic functionalities of the level working. Up to now I've managed thanks to you wonderful people but now I've got stuck and I am looking to you for help! (as my programming skills suck)
I need an elevator working and so far I've got this code which works fine, however i would like to develop this script a bit further by adding some script that only makes this script valid when in a trigger zone. if you understand?
basically I only want this script to apply when I'm in the elevator, otherwise this script works even when I'm on the other side of the map!
Also I would like the whole animation to play before I can play it again.
Any and all help will be greatly appreciated by myself and my team, Thank you!
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;
}
}
Answer by Lovrenc · Aug 04, 2013 at 11:49 AM
Put interactive scripts on your player, not on objects you want to controll. That way, when user presses E, you can go trough objects in your proximity (be it by triggers, raycasting,...) and execute calls you need.
That way, you will greatly reduce number of Update functions, you will have "interaction" code on one place and it will be easy to execute a lot of different actions (depending on objects "E" keypress relates to).
About your question. If you will leave it as it is, you can use "Box collider (mark it as isTrigger)" on your elevator. When players triggers it, you remember it until he exits. So when "e" is pressed, if player is in the trigger zone you react, otherwise you exit function without any action. BUT THIS IS REALLY BAD DESIGN.
Why is it bad design to add a box collider? The way you explain means you control everything from one script,why not. But that kinda goes against the idea of each object controls its behaviours. If you need maintain your script, if something is wrong with the platform, you look for the platform, in your case, you look for the few lines among hundreds where you deal with the platform.
That means if a dog needs bark when you are around, you put that on your player script as well, right after the platform lines? That does not sound good to me.
No, elevator still controlls its own actions. But he provides functions to be called and player calles them. It makes sense (in my opinion, but then again i am by no means great developer) because he wants the player to interact with it by pressing "E" key. So i don't see any meaning in elevator checking for keypress events.
Your dog example is totally different beast and would make no sense to force bark code in player script. If however player would need to "Give a treat" to a dog for him to bark by pressing "E", i would put:
$$anonymous$$eypress logic to player. Then check what object to apply it to and call "Interaction" interface function (and maybe send parameters if needed, depends on type and number of different actions). Then it is up to dog to do his part.
I might be in total darkness here so please comment on this further.
Could you please further explain your method?
The way I'm understanding what you've said is that I should be applying the script to the player, and then through use of triggers it can detect the players input in the trigger area and if its a match (say E is to play the animation) it will play the animation, then once the player is out of the trigger area pressing E will no longer effect it?... is this correct or am I way off course? haha
Plase wait, maybe my pattern is bad and i dont want to $$anonymous$$ch bad practices.
Answer by JakeParrott · Aug 04, 2013 at 01:47 PM
http://www.youtube.com/watch?v=HfX6LsDGY2g
I am about to follow this guys video tutorials. its not exactly what I'm looking for, I would have preferred a key press function (maybe I could incorporate that into this somehow I'm not sure)
But thanks for your help :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Learning C# instead of doing what tutorials say... 2 Answers
C# Targetting Script 2 Answers
Need Coding Help! BCE0044: expecting }, found ''. 1 Answer
New to unity and any coding for that matter, need advice!!! 3 Answers