Question by
Ninjalord8 · Sep 27, 2015 at 10:00 PM ·
javascriptraycast2d gameraycasthitphysics.raycast
[fixed] My physics.raycast vision doesn't work as I expect it to, and I don't know why
(Leaving this up for the sake of anyone else making the same mistake as me.)
I've spent several hours trying to get this to work; looking at forums and other people's issues, and I still can't figure it out.
#pragma strict
var minPlayerDetectDistance = 0.5;
var fieldOfViewRange = 20;
var thief : GameObject; // Not being used *yet*
var r : int = 3;
function FixedUpdate () {
FOV(thief);
}
function FOV(playerObject : GameObject) : boolean{
for(var i = -45; i<=45; i+=5) {
var x = (r*Mathf.Cos(i * Mathf.PI / 180));
var y = (r*Mathf.Sin(i * Mathf.PI / 180));
var hit : RaycastHit;
var rayDirection = transform.TransformPoint(Vector3(x, y, 0));
if (Physics.Raycast (transform.position, rayDirection, hit)) {
Debug.DrawLine (transform.position, hit.point, Color.cyan);
if (hit.transform.tag == "Player") {
Debug.Log("Can see player");
}else{
Debug.Log("Can not see player");
}
} else {
Debug.DrawLine (transform.position, rayDirection, Color.cyan);
}
}
}
-When too far, it doesn't actually collide.
-It doesn't collide with the actual corners, and when it does collide it makes the hit point raised, and spreads it out oddly.
I would really appreciate any help with getting it working, or anything that I should do better.
ss2015-09-27at080709.png
(48.7 kB)
ss2015-09-27at080840.png
(44.1 kB)
Comment
Best Answer
Answer by Ninjalord8 · Sep 28, 2015 at 06:16 PM
I noticed that the rayDirection was actually rayPoint, and needed to be converted to a direction. This created an issue where it went in the wrong direction.