:Raycast() returns nil

hello, ive been making sentry gun for my game, but there’s issue
i need to actually find can turret see enemy before shooting in they direction
so i used workspace:raycast() method. but it ALWAYS returns nil. FindPartOnRay() too.
sentry uses OOP module to control it

code and output screenshots:


изображение

self.position source:
изображение

PrimaryPart isnt nil

use a while loop to keep on checking ray

It might be because you’re not normalising the direction with .Unit or the direction calculation might be wrong.

Also you don’t need to specify RaycastParams if you haven’t set them to anything, you can remove that part.

and I don’t see the lookat variable being used anywhere, so you should probably remove it.

2 Likes

Why would he need to normalise the vector? The direction of a vector doesn’t depend on its magnitude.
You’re right about the RaycastParams thing and the lookat variable.

It doesn’t make sense if u do LookVector.Unit because a LookVector is already normalised

If a vector is not normalised, it can sometimes cause anomalies and distance issues.

Unit normalises the magnitude to 1, so you can then multiply by the distance, e.g 100, if the starting value is not 1, it might overshoot or undershoot the distance. Causing unreliable results.

It’s just good practice imo. I usually use lookvectors or something like this:

local direction = (endpos - startpos).Unit * 100

First time I hear about this. Again, it makes no sense to me. Is there any official resource saying this?

Change the direction variable to this local direction = ((workspace.sentry.Model.PrimaryPart.CFrame.LookVector * self._ai.look_distance) - self.position).Unit * self._ai.max_distance

There is no official resource that I know of.

Ig you can get away without normalising look vectors, but for any other vector, its necessary.

It’s necessary only when working on magnitudes, but for obvious reasons. When determining the position of a particle, (3,2) isn’t the same as (6, 4). The same goes for magnitudes of velocities, that’ll be different.
For vectors utilised only for determining the direction that’s always possible, instead. Those 2 vectors would be perfectly fine, both, since when calculating the direction of a vecgtor you can’t NOT normalise, as you should be calculating the angle at which something is shot. That implies calculating the ratio between the x and y components, meaning that it’d remain just the cos and sin of the angle, without any reference to the magnitude.

sure, direction doesn’t technically require normalisation for directions.

But using .Unit helps with consistency, and wont return false-positives as the distance is always going to be the same.

Its not always necessary to normalise, its just to keep everything consistent and its a good habit.

But again, this is NOT a matter of distance, nor consistency. If one uses the look vector there is no issue. If we’re using 2 position vectors to determine the direction it’s easier to do so, since the calculations done by the engine are always the same (when you raycast the engine is gonna have to do angle calculations, the same as normalisation; instead of doing them twice let the engine do them once). Instead of adding extra code and making it harder to read (well, not even that much tbf, a .unit won’t give too many head aches) let’s keep it simple.
I don’t see how a false positive could ever be returned.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.