Raycast hit.point differs from Ray direction [SOLVED]
[EDIT] Messed up with Raycast's parameters : for direction I put a position, not a true direction == player.transform.position - eyes.transform.position
Instead of just player.transform.position... Noob mistake //END
Hello everyone,
My enemy shoots a raycast from his eyes to the player's position (cible). It uses a Ray "viseur" (see script).
As you see on the picture, the yellow line, which is the same as the Ray viseur, is correctly oriented to the player.
The cyan line is the hit.point, which is totally abnormally hitting the wall, wrong direction. But the Raycast uses the Ray, so how is that possible??? The enemy is in IgnoreRaycast layer.
I'm lost... Thank you for your help.
if (angle < fieldOfView * 0.5f) //angle between enemy and player
{
Debug.Log("angle is right");
RaycastHit hit;
Vector3 cible = player.transform.position;
cible.y += 2; //adjusting height
Ray viseur = new Ray(eyes.position, cible);
if (Physics.Raycast(viseur, out hit, range))
{
Debug.Log(hit.collider.name);
Debug.DrawLine(eyes.position, cible, Color.yellow);
//this Line is drawn between eyes of enemy and player (cible) position
Debug.DrawLine(eyes.position, hit.point, Color.cyan);
//this line is between eyes of enemy and hitpoint, they should logically be the same
Debug.Log("Raycast fired");
if (hit.collider.tag == "Player")
{
Debug.Log("Raycast touched player");
inSight = true;
chase = true;
agent.speed = chaseSpeed;
}
}
}
I've been trying with direct transform.positions, even without passing through the Ray... all the same weird thing happening.
It shoots from the right place but the player position is wrong when it goes through the Raycast function.
And when testing the Ray, it has the correct DrawLine (yellow).
Answer by mrsev · Sep 01, 2017 at 08:23 AM
I have part of the answer.
The goddamn mesh I made it with Blender, which goddamn imports it with rotation problem.
So I corrected the rotation putting the mesh as a Child of an EmptyObject.
Now if I Raycast to forward, it goes to the right of the enemy, so there's a bug with relative Player position or what...
Your answer
Follow this Question
Related Questions
RayCast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) question 0 Answers
How to cast a Ray using local pivot of an object? 1 Answer
Raycast Hit suddently stopped working 0 Answers
Physics Raycast not working 1 Answer
Disable a UI element when ray doesn't hit it anymore. 1 Answer