- Home /
Raycast not pointing correctly with a custom Vector3
Hi, I have this little issue with Raycast, I want it to point to a custom Vector3, but it is not working correctly. It keeps pointing to the nowhere. Here is my code:
var tr : Transform;
var mask : LayerMask;
var rayHit : RaycastHit;
var aimAt : Vector3;
function Start () {
tr = transform;
aimAt = Vector3 (0.2, 1.5, 65);
}
function Update () {
if (Physics.Raycast (tr.position, aimAt, rayHit, Mathf.Infinity, mask)) {
Debug.DrawLine (tr.position, rayHit.point, Color.blue);
}
}
If I use "tr.forward" insetead of "aimAt" it works fine pointing to the front:
if (Physics.Raycast (tr.position, tr.forward, rayHit, Mathf.Infinity, mask))
Anyone knows why this is happening?
Comment
Best Answer
Answer by BBoulder · Jan 30, 2013 at 03:55 AM
For Raycast, you give it a position and a direction. You're giving it a position and another position right? Just do a subtraction, aimAt - tr.position. Or you can use linecast.
Oh my God. It worked with the subtraction. Thank you, so much.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091831im_/https://answers.unity.com/themes/thub/images/avi.jpg)