Hello World,
I have a problem that I can’t figure out how to fix. I have the following code:
if (!Physics.Raycast(tpos,new Vector3(to_x,to_y,tpos.z)))
print("Straight shot!");
tpos is a Vector3, in this case transform.position, and to_x and to_y are the destination x and y coordinates (z is the same as it’s a top-down game). the issue is that it always thinks that it is a “straight shot”. I have tried:
-Apply a ridgidbody to one of the objects
-Confirming that a collision should occur (it should)
-Applying transform.TransformDirection to the “new Cector3”
But no luck. Any thoughts?
You have a “!” in there which means “not”. You’re saying “if the Raycast from tpos to dest does not hit, then print straight shot”, which is probably the opposite of what you want… just remove the “!”
@Wings: Thanks for the feedback, it’s correct. If there is not and object between this point and that point, then it’s a straight shot.
Oh, I see, that makes sense, and I see where the mistake is. You should use Linecast instead of Raycast. Raycast takes an origin and a direction, whereas Linecast takes an origin and destination and sees if there’s anything in between them.
did the thing had a collider added?