Hi, I was wondering how can I detect range? What I am trying to do is when my player character comes within range of the door on a house, then do something.
My Player:
//Is made off the first person controller dragged into the hierarchy.
Player (FirstPersonController dragged onto a “Player” named prefab.)
graphics (Part of the FirstPerson Controll)
CameraHead (Empty GameObject)
I have a script attached to my player to make the camera follow the player by having the same transform position as of “CameraHead”.
This is my “CameraScript”
using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to the player and it
/// causes the camera to continuously follow the
/// CameraHead.
/// </summary>
public class CameraScript : MonoBehaviour {
//Variables Start___________________________________
private Camera myCamera;
private Transform cameraHeadTransform;
//Variables End_____________________________________
// Use this for initialization
void Start ()
{
//A quick reference of the CameraHead's transform.
myCamera = Camera.main;
cameraHeadTransform = transform.FindChild("CameraHead");
}
// Update is called once per frame
void Update ()
{
//Make the camera follow the player's cameraHeadTransform.
myCamera.transform.position = cameraHeadTransform.position;
myCamera.transform.rotation = cameraHeadTransform.rotation;
}
}
Now what I want is to detect if my player is within range of the door of the house (the door has it’s own script attached.
using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to the front door on the House.
///
/// This script creates the ability to enter the House by when in
/// range, a message appears "Press "O" to enter.", and if
/// clicked load the InsideHouse level.
/// </summary>
public class EnterHouseScript : MonoBehaviour {
//If within range the PlayerRange script will set this
//boolean to true.
private bool withInRange = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(withInRange == true)
{
//Just a dummy function to test if I am in range
print("You are within range");
}
}
}
What do I need to add, what script should I add it too, and what gameobject should I attach it too?