Issues With Raycasting

I’m somewhat new to lua so I’d like some help with a piece of code that has been bothering me for a while.

ThePosition = Ray.New(position, position2)

I know you can’t just make the Velocity the Ray, but what’s the point in making a ray when you can’t put it into the Velocity? How do people do that?

BVelocity = Instance.new(“BodyVelocity”)
BVelocity.Velocity = ???

I’m oblivious to how this is supposed to work when using rays. Some people say use ‘CFrame.new(Position1, Position2).p’ and that somewhat works, but it would also make the projectile shoot in random directions whenever something is in the way of where you’re aiming.

Any help or information (or even advice) on this issue I’m having is definitely appreciated.

1 Like

I’m not exactly sure what you’re trying to do here, I might be able to help you further if you could tell me a bit more on what you’re trying to make here.

But with that, raycasting basically casts a ray at an origin position, at a given direction, with a specified length. It doesn’t draw a ray that physically keeps going like a bullet, it’s basically a ray in geometry. It expands infinitely in a given direction from the origin point. (but like I said you can specify the length of the ray here.)

This issue has also been really hard for me to explain so sorry if I’m not giving enough detail.

Basically what I mean is that I want to use a ray for a bodyvelocity (because people tell me to use rays whenever I ask how it’s done), but I just don’t understand how to do it the right way. When I first tried (I know this is not too smart) I used ‘BodyVelocity.Velocity = Ray.new()’ which didn’t work for me as it gave an error.

If you’re trying to give a part velocity to go towards a brick for example, we can generate that angle and use that instead of creating a ray.

So if that’s what you’re trying to achieve you can use a simple formula like this:

bodyvelocity.Velocity = (part1.Position - part2.Position).unit * SPEED

EDIT: It doesn’t even have to be a physical part, this formula should remain the same. You put in two points and it’ll get the direction from that, and you can then multiply it by the speed.

2 Likes

Thanks for the time and effort put into giving me a solid solution. Respect much thanks!

1 Like