- Home /
Ray from enemy to player is always a bit off (Image)
I've got a ray from the enemy to the player, and a line showing the ray in unity. The ray always seems to fire just to the side of the player, so it never actually hits.
Here is my code:
var rayDirection = other.gameObject.transform.position - transform.position;
rayDirection.y += 1;
var currentPosition = transform.position;
currentPosition.y += 1;
var hit : RaycastHit;
Debug.DrawLine (currentPosition, rayDirection, Color.yellow, 3);
if(Physics.Raycast(transform.position, direction, hit, viewDistance))
{
print ("Sent Raycast");
Debug.DrawLine (transform.position, hit.point, Color.cyan, 3);
if(hit.collider.gameObject.tag == "Player")
{
print("Player found");
}
}
So I'm wondering why this is happening?
Answer by Slobdell · Jun 21, 2013 at 09:11 PM
What is that math you're doing for the raycast direction(which is actually a vector3 location not a direction)? If you want the raycast to shoot toward and object, give it only the objects position. Why are you subtracting something else from it?
I was under the impression that it was a direction.
The code works:
if(Physics.Raycast(transform.position, direction, hit, viewDistance))
Actually, I take that back. That code fixed the DrawLine, but did NOT fix the raycast. In fact, the Raycast does seem to need a direction, not an end point.
Your answer
Follow this Question
Related Questions
Raycast gun 2 Answers
The name `target' does not exist in the current context 1 Answer
Hit distance Change light range 1 Answer
How to hit two object with one raycast? 2 Answers
Need help with Third person shooter 0 Answers