- Home /
Problem with raycast
I'm trying to detect if there is something between my camera and my character. Here is my code:
void Update ()
{
myTarget = target.position + new Vector3(0, offsetY, 0);
length = Vector3.Distance(transform.position, myTarget);
//Shoot Raycasts
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, myTarget, length);
Debug.DrawLine(transform.position, myTarget, Color.yellow);
Debug.Log("Number of hits: " + hits.Length);
}
Where myTarget is my character (I'm using an offset to point the center of it). I'm using length for not to detect the floor.
Here is a screenshot of my scene:
Ass you can see, there is something in my raycast trajectory, but it's not being detected... Any idea?
Answer by robertbu · Aug 19, 2013 at 02:15 PM
Your problem is on line 8. You are using a world position for direction. Replace with:
hits = Physics.RaycastAll(transform.position, myTarget - transform.position, length);
Answer by DaveA · Aug 19, 2013 at 01:40 PM
Make sure your objects have colliders on them. Make sure they are not in a 'no collide' layer.
Those are default boxes (with their box colliders and layer=default).
Your answer
Follow this Question
Related Questions
Raycasting To Objects 1 Answer
C# More Accurate or Larger Raycast 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
AI sight script not detecting player 0 Answers
C# Raycast isn't working? 1 Answer