- Home /
Doors and Multiple Scenes - 2D
I'm making a 2D PixelArt Survival-Horror/Platformer, and I just got death and respawning to work, but: http://i.imgur.com/vLFh2j1.png
How do I add this:? I want to make it so when you're in proximity of the door, you press E and then an animation plays when the door opens and then it fades to black and fades back and you're in next scene.
It sounds simple probably like:
play animation ("Door_Open")
fade to black
load scene ("Scene_02")
fade back
Please, clarify what I have to write, please. Thanks!
Answer by sparkzbarca · Mar 01, 2015 at 04:45 PM
so it depends on what you want it to look like, yes your right
create a new animation called door open, make it then do doorOpen.play() to open the door.
actually for this simple a thing you dont have to use animations persay. animation is just code after all. you can actually just write out the code to open the door.
a door opens along it's y axis edge normally 90 degrees.
create a "door" or rectangle just for testing purposes.
add a script that basically says this.
//while close enough, there are a lot of ways to do this the easiest is a distance check
if (vector3.Distance(player.transform.position, door.transform.position) < MaxDistance && input.getbuttondown "Action Key")
//you'll notice i use getbuttondown, not getkey or getkeydown or getbutton. //thats because you shouldn't use getkey, it prevents you from easily redefining the input, //you don't use getbutton or getkey because the door would open multiple times unless the player //clicked REALLY REALLY fast, get key/button is for holding stuff down, get x down is for //whenever a player should only click a key not ever hold it down. //if you want E to be the default key for action key or whatever you want to name it. //go into unity, edit->project settings-> input, thats the list of buttons and names and such //add a new input by changing size or take over an existing one and make it so the name is //action key or whatever you wish to call it and the positive button is e {
//now we rotate the door, first we find the doors edge, thats simply from the middle of the door to one half the width of the door. You could also create an empty game object placed on an edge and rotatearound that. one half an objects size is so common its built in, its called its extents.
vector3 point = door.transform.position + new vector3(door.collider.bounds.extents.x,0,0);
//this is a point on an edge, hopefully you can see how that works, we take the doors position /and move along the x axis half the width of the door, if we'd like to get the opposite edge //we - instead of + the value.
//rotatearound takes a point, an axis, an angle //our point is what we just found, our axis is the Y axis, up and down because thats the axis //doors rotate around, our angle is 90 because a door opens 90 degrees.
door.transform.RotateAround(point,door.transform.up,90);
//except this opens the door instantly, you can lerp to rotate slowly by changing that 90 to //a variable like time.deltatime and that will rotate it a small amount each frame, then you want //to just check the angle and if its over 90 stop.
//alot of explanation here but its like 4 lines of code, i just want to make sure you understand //what your doing and aren't just regurgitating my code.
Okay, I'll try that. I'll tell you if I get any errors.
Alright, so I'm about to test it. It probably won't work cause I suck at coding, but here's what I got (BTW, I am trying to learn, so I read like everything you wrote twice)...
#pragma strict
function Start () {
}
function Update () {
if (vector3.Distance(player.transform.position, door.transform.position) < $$anonymous$$axDistance & input.getbuttondown ("Action"));
{
vector3 point = door.transform.position + new vector3(door.collider.bounds.extents.x,0,0);
door.transform.RotateAround(point,door.transform.up,90);
}
}
Alright, so, I limited the errors I'm getting down to 1, and it's saying that on line 10, it's expecting a semicolon at the end, but I can see one there. xD
Tell me whether or not that's right, meanwhile, I'll test it.
BTW, where do I add the scene change in all this?