I’m making a basic hide-and-seek like game. I have a seeker that fires rays at hiders, when the ray hits the hider they are found. Currently i have to be about 5-10 units in range of the hider before the ray will hit them, but it DOES hit them within that range so i don’t believe the ray is hitting any other collides first. This is the code i am using.
fus = GameObject.Find("Fus");
Vector3 direction = fus.transform.position - transform.position;
RaycastHit hit;
Ray sightRay = new Ray (transform.position, direction.normalized);
Physics.Raycast (sightRay, out hit, Mathf.Infinity);
hit.collider.gameObject.GetComponent<Animation> ().Play ("wave");
Debug.Log ("You Have Found " + hit.collider.name);
No other collider is being logged in the console.
You are drawing Debug.DrawRay(…) with length of 1. Of course it will be short. See manuals:
and transform.forward is a unit vector.
There’s nothing wrong with Raycast here though.
Besides, you draw debug ray into transform.forward while you raycast into (fus.transorm.position-transform.position).normalized, so what does that debug ray show? Certainly not where you raycast to.
I pasted the wrong drawray in the snippet by mistake. Still, I have the hiders set to wave when they are hit, the animation will not play until I get up close using the ray code above. On mobile. Sorry for typos.
Let me recheck this part: what exactly do you get in output from
Because if you don’t get NullReferenceException, ray does hit something before it hits your hider.
Hmmm, im not getting a NullReferenceException.
Is there anything inside the standard asset Fps-controller that it could be hitting?
Here’s a video of the behavior I am getting and the full code.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Sight : MonoBehaviour
{
public float FOVAngle = 60f;
public List<GameObject> hiders = new List<GameObject> ();
public GameObject closetsHider;
public float distanceToClosest;
// Use this for initialization
void Start ()
{
hiders.AddRange (GameObject.FindGameObjectsWithTag ("hider"));
}
// Update is called once per frame
void Update ()
{
distanceToClosest = Vector3.Distance(closetsHider.transform.position, transform.position);
FindCLosetsHider();
foreach (GameObject i in hiders) {
Vector3 direction = i.transform.position - transform.position;
float angle = Vector3.Angle (direction, transform.forward);
if (angle < FOVAngle * 0.5f) {
ShootRays(direction);
}
}
}
void ShootRays (Vector3 direction)
{
RaycastHit hit;
Ray sightRay = new Ray (transform.position, direction.normalized);
if (Physics.Raycast (sightRay, out hit, Mathf.Infinity)) {
if (hit.collider.tag == "hider" && !hit.collider.GetComponent<hider>().hasBeenFound) {
hit.collider.gameObject.GetComponent<Animation> ().Play ("wave");
Debug.Log ("You Have Found " + hit.collider.name);
hit.collider.GetComponent<hider> ().hasBeenFound = true;
hiders.Remove (hit.collider.gameObject);
}
}
}
void FindCLosetsHider ()
{
foreach (GameObject i in hiders) {
if (!i.GetComponent<hider> ().hasBeenFound) {
if (Vector3.Distance (i.transform.position, transform.position) < Vector3.Distance (closetsHider.transform.position, transform.position)) {
closetsHider = i;
}
}
}
}
}
This is just a “make anything” busy work assignment that my tutor gave us over spring break.
I feel like im trying to do too much everyframe.
CharacterController is capsule collider+its controls which you might be hitting if you raycast from inside it.
When you ask hit.collider.name if you didn’t hit anything, then hit.collider will be null. So asking null.name will give you NullReferenceException in that Debug.Log line, that’s why I’ve pointed that out.
You’re doing too much only when compiled version for target platform is not running full FPS. Before that, don’t optimize unless your game is already finished.
1 Like
I finally fixed the issue in case anyone else ends up here.
I changed the line Ray sightRay = new Ray (transform.position, direction.normalized);
to Ray sightRay = new Ray (transform.position + transform.up, direction.normalized);
I guess I was raycasting from my feet and colliding with the ground. Not sure why I had no evidence of that in the console though.