- Home /
Raycasting doesn't seem to "hit" a Character Controller? [image]
I have code to check to see if an enemy can "see" the player. Unfortunately it seems that the Raycast isn't hitting the player, who has a Character Controller attached.
I've googled, but none of the other answers seem to answer this. Any idea why Raycast wouldn't hit the player?
[the yellow line is the Line drawn before the raycast, the cyan line is the line drawn between the enemy and the "hit" object. If the palyer was being hit, you'd see it in the consol and a lot of cyan lines, equal to the yellow lines.]
function OnTriggerStay (other : Collider)
{
if (other.gameObject.tag == "Player")
{
var rayDirection = other.gameObject.transform.position - transform.position;
rayDirection.y += 1;
var currentPosition = transform.position;
currentPosition.y += 1;
var hit : RaycastHit;
Debug.DrawLine (currentPosition, other.gameObject.transform.position, Color.yellow, 3);
if(Physics.Raycast(currentPosition, rayDirection, hit, viewDistance))
{
Debug.DrawLine (currentPosition, hit.collider.gameObject.transform.position, Color.cyan, 3);
print ("Hit: " + hit.collider.gameObject.transform.name);
if(hit.collider.gameObject.tag == "Player")
{
print("Player found");
}
}
}
}
Answer by Owen-Reynolds · Jun 21, 2013 at 11:29 PM
Raycasting is tricky, and you should have a feel for Vector3 math and offsets vs positions. For example, adding +1 to rayDirection.y changes the angle by a semi-random amount (which is usually not what you want.)
I'd say rewrite lines 5-8, by figuring out where you want to shoot from, and where you want to shoot to. Then the last line computes the ray using computedTarget-ComputedStart.
Also, Debug.DrawRay doesn't use the same math as Raycast. Close, but not the same. So you can have where drawRay seems to hit/miss, but the raycast is different.
That was it -- thanks. Here is the working code:
function OnTriggerStay (other : Collider)
{
if (other.gameObject.tag == "Player")
{
var shootTo = Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z);
var shootFrom = Vector3(transform.position.x, transform.position.y + 1, transform.position.z);
var rayDirection = shootTo - shootFrom;
var hit : RaycastHit;
Debug.DrawLine (shootFrom, rayDirection, Color.yellow, 3);
if(Physics.Raycast(shootFrom, rayDirection, hit, viewDistance))
{
Debug.DrawLine (shootFrom, hit.collider.gameObject.transform.position, Color.cyan, 3);
print ("Hit: " + hit.collider.gameObject.transform.name);
if(hit.collider.gameObject.tag == "Player")
{
print("Player found");
}
}
}
}
shootTo and shootFrom can be simplified.
The way you were originally adding math is also fine: shootFrom=transform.position
then shootFrom.y+=1;
Answer by Slobdell · Jun 21, 2013 at 10:39 PM
Dude this is at least the 2nd time today you've posted the same question, thought I saw a third. Please don't repeat. Again, I will ask you what is that calculation for raydirection doing? Why do you think that would point to your object? This is copy and pasted straight from the docs.
public class example : MonoBehaviour {
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10)){
print("There is something in front of the object!");
}
}
}
And since I know you will ask, change Vector3.forward to your object you want to shoot the ray at
Two questions. The first turned out to be a question of Debug.DrawLine (the Yellow line).
This question is about the raycast (Cyan line) not hitting the player. The cube is hit when the player is behind the cube.
The calculation should give the direction required to point at the player. The image below is when I use transform.TransformDirection(Vector3.forward)
-- as expected, the ray only shoots forward of the enemy, so only hits the player when the player is in DIRECT line of site. I'm attempting to find out if the player is in the field of view, not line of site.
And this is what happens when I use hit.collider.gameObject.transform.position
-- the blue line (raycast) shoots behind the enemy, not towards the player.
Did you not see my comment that says change vector3.forward to the position of your object you're trying to hit?!?!?!?!