Ok so i have a script in which when the player is so far from the door and presses an Interact key the door they are standing front of opens. Every thing i have seems to be working just fine, except the door wont open without the player being right in front of the door, and i don’t know what to do.
If someone could help me figure this out that this would that would be awesome.
using UnityEngine;
using System.Collections;
public class Door : MonoBehaviour {
private float distance;
private GameObject player;
private Ray ray;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
}
void Update () {
distance = Vector3.Distance(transform.position, player.transform.position);
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetButtonDown("Interact") && distance < 20 && Physics.Raycast(ray, out hit) && hit.collider.gameObject.tag == "Door") {
animation.Play("DoorOpening");
}
}
}