Hey guys,
I have script that checks if you are near a door and if you are within a certain range, the script returns the variable ‘InRange’ true. This variable is a public variable in this script. Then I have another script that opens the door when you pressed E and ‘InRange is true’. Now the problem is, if I have multiple doors in my scene and I am near door 1, InRange is returned true, but because I use this variable for every door, also door 2 and 3 think that I am near them while this is not true. So when I press ‘E’, all the doors open at once. How can I make it so that only the door I am really InRange of opens. So like how can I make a variable that has one name, but has a different values on the different doors (with each assigned an individual script to them)
Thank you so so much!
This is the code for checking if you are near the door:
//INSPECTOR SETTINGS
public float Range = 5; // Within this radius the player is able to open/close the door.
public bool InRange;
//DEBUGGING
public Color DebugRayColor = Color.red;
//START
void Start()
{
}
//UPDATE
void Update()
{
InRange = false;
Ray ray = Camera.main.ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0F)); // Set origin of ray to center of screen and direction of ray to cameraview.
RaycastHit hit; // Variable reading information about the hit.
if(Physics.Raycast (ray, out hit, Range)) // Casts a ray from the center of screen towards where the player is looking.
{
if(hit.collider.tag == "Door") // Change the tag you use for your doors here.
{
InRange = true;
}
}
this is the code for rotating the door:
//UPDATE
void Update ()
{
//Access InRange variable from raycasting script
GameObject Player = GameObject.Find("Player");
RayCasting raycasting = Player.GetComponent<RayCasting>();
if (raycasting.InRange == true) {
InRange = true;
}
else InRange = false;
//DEBUGGING
//print (InRange);
if (Input.GetKey(KeyCode.E) && InRange == true) {
Openable = true;
}
if (Openable)
{
//Rotate();
}
}
}