Hello everyone.
I am 3D modeller more than a programmer. But now I need several doors in my scene to be opened and closed.
What I have. Two doors. One animator controller attached to these doors. A couple of clips placed in this animator controller, that is for opening rotation and closing rotation.
And DoorController script.
First I had an issue in which all of my doors were opened in one moment when I have triggered an OpenDoor animation. I resolve this problem by using a call to a certain object that Raycast hitting with, instead of just getting a local Animator object.
But what problem I have now is: When I open one of the doors, my program assign a string value “Opened” to doorState parameter of ALL OF THE DOORS IN THE SCENE. This makes rest of the doors playing the wrong animation after all (because they still closed in real).
The question is: How can I use my script with only one instance of a door and store unique parameter’s values for this only one instance. How can I send command in this script to only this one certain door, not to everyone that have this script attached? How can I call to Animator component of this only one instance of a door? Not to everyone that have this script attached.
public class DoorController : MonoBehaviour {
//Animator m_animator;
public Camera m_camera;
public float maxDistance = 2;
private string doorState;
RaycastHit hit;
// Use this for initialization
void Start () {
//m_animator = gameObject.GetComponent<Animator>();
doorState = "Closed";
}
// Update is called once per frame
void Update () {
if (Physics.Raycast(m_camera.transform.position, m_camera.transform.forward, out hit, maxDistance))
{
if (hit.transform.tag == "Door")
{
if (Input.GetButtonDown("Use"))
{
OpenDoor();
}
}
}
}
void OpenDoor()
{
if (doorState == "Closed")
{
// Open a door
//m_animator.SetTrigger("OpenDoor");
hit.transform.GetComponent<Animator>().SetTrigger("OpenDoor");
doorState = "Opened";
}
else
{
// Close a door
//m_animator.SetTrigger("CloseDoor");
hit.transform.GetComponent<Animator>().SetTrigger("CloseDoor");
doorState = "Closed";
}
}
}