- Home /
Problem With Physics.Raycast
Hi, I am trying to create a Ray from my MainCamera to my Player
Here is the code I used...
var target : Transform;
function Update ()
{
Debug.DrawRay(transform.position, target.position);
if (Physics.Raycast(transform.position, target.position))
{
Debug.Log("Something in the way");
}
}
Now I got it to work and all, but it doesnt seem to want to point towards the target! It points off at an angle, I need it to point directly down towards the Target.
Here are a few pics to show what I mean...
Any help or suggestions would be greatly appreciated!!!
Answer by Owen-Reynolds · Mar 06, 2013 at 04:46 PM
The second input(*) of a raycast isn't the place to look, it's the direction to look. In your case, should be target.position-transform.position
Is is a little confusing, since LookAt(player.position)
does work -- the input is an actual map spot. In general, you always have to think "does it want an actual spot on the map, or a direction going to that spot?"
(*)Raycasts can either take two "start here" + "look this way" inputs, or a single Ray. But the two parts of the ray are "start here / look this way," so it's the same either way.
No luck, I got the same result either way, tried putting it like this
if (Physics.Raycast(transform.position, target.position))
{
Debug.Log("Something in the way");
}
and like this
if (Physics.Raycast(target.position, transform.position,))
{
Debug.Log("Something in the way");
}
The little dash wasn't a typo. It really was a $$anonymous$$us sign. It might help to look at a bunch of examples of other people using ray casts.