Hi, so I have a game where the player opens the door and it instantiates a prefab room on the other side on opening. I’m running into issues, however.
First off, I cannot get the prefab to instantiate at a certain position and angle, matching the door being opened. It always instantiates at 0,0,0. I’ve been reading the documentation on the instantiate property but the problem is that every time I put in the transform, quaternion, and other parameters I’m being told to include, Unity yells at me and I have no idea why when I’m pretty much doing it exactly as listed. I figure if I can make it inherit the transforms & rotation of the game object, it would mostly work and allow me to + or - for adjustments, but I can’t seem to even get that to work since it won’t let me call its transform properties or properly save them as a variable.
Second, I have no idea how to properly use the the GetComponent variable. Originally, I wanted to use a separate script to generate/instantiate the room by checking a variable in another script. The problem is I could never get the GetComponent function to work and have no idea how works, despite some examples I’ve found. To remedy this, I put the instantiation in the opening door script. It works (for now at least; it used to be spawning rooms infinitely) but it seems to be causing a glitch by making the door no longer open completely (It only moves a fraction of an amount its supposed to so it only twitches, and yes, I am taking into account the room being instantiated on top of it), so I think it may be best if its handled on another script.
Here’s some code to illustrate the gist of what I’m doing.
if(open){
//Open door
if (!opening) {
Instantiate(Room_Prefab);
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
open = true;
opening = true;
}
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
opening = false;
}
Any ideas? I’m not very good with code yet. How would I write this if I wanted to put it in another script.