- Home /
How to detect range?
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.
>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;Player (FirstPersonController dragged onto a "Player" named prefab.)
using System.Collections;
/// /// This script is attached to the player and it /// causes the camera to continuously follow the /// CameraHead. ///
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;
/// /// 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. ///
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?
You will want to create a Raycast from the player to the object (door) http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-distance.html
Answer by Klakwa · Dec 07, 2012 at 07:03 PM
You can do that in several ways (as always).
If you want to track player by the door you can simply reference the player's transform.position and then compare it with the position of the door by using Vector3.Distance(). And then check if the distance is within the range or not.
Vector3 doorPosition;
Vector3 myPosition;
int range = 10;
if(Vector3.Distance(myPosition, doorPosition) < range)
{
//Do something
}
You can also use Raycasting to cast rays from the player and see what they hit and what's the length of the ray. Using the Physics.Raycast() and then checking out the RaycastHit.distance or RaycastHit.collider.transform.position and compare it with the Player's position.
But I would probably use a collider as a trigger to simply trigger the event on when character goes near it. It simply eliminates the reason to look for the event to happen on each frame and it's easy to change and manipulate if you are using it for only one event.
So then you do it like that:
OnTriggerStay(Collider objectWeCollideWith) // if you want the event to be called each frame the player is within the range
{
if(objectWeCollideWith.tag == "Player")
{
// Do something
}
}
If you want to activate more objects at once you can try the other way around. Have player to use Physics.OverlapSphere(playerPosition,range) to check out what colliders he's colliding with within certain range and then you can walk the list of them and perform some action. Like that:
void CheckNearObjects()
{
Collider[] objects = Physics.OverlapSphere(transform.position, range);
foreach(Collider object in objects)
{
if(object.tag == "Door")
{
object.DoSomething();
}
}
}
Hope that helps.
If you are using the collider as trigger you can also do it in another way not using OnTriggerStay() method, but ins$$anonymous$$d you can turn an event on while object enters the trigger using OnTriggerEnter() and then turn the event off using OnTriggerExit().