I’m trying to make a script which allows a player to enter a vehicle, so far I have this code which should make a message pop up when the player is near the vehicle game object. However, the message always appears no matter the distance. Any help would be greatly appreciated, thanks.
using UnityEngine;
using System.Collections;
using System;
public class VehicleEntering : MonoBehaviour {
public float threshold = 1;
public static Transform playerObject;
public Transform playerObjectInspector;
public static Transform vehicleObject;
public Transform vehicleObjectInspector;
private void Awake() {
playerObject = playerObjectInspector;
vehicleObject = vehicleObjectInspector;
}
void Update() {
float distance = Vector3.Distance (VehicleEntering.playerObject.transform.position, VehicleEntering.vehicleObject.transform.position);
if (distance <= threshold) {
OnGUI();
}
}
void OnGUI() {
GUI.Box(new Rect(0,0, Screen.width, Screen.height), "Press E to enter vehicle");
}
}