Roblox equivalent to Unity's Vector3.Project?

I’m trying to make a tornado and use velocity to move parts towards a Vector3 which would be more optimised than using LineForces or anything else. I saw that Unity’s Vector3.Project could do this, is there a roblox equivalent or do I have to find a way to make a function like this myself?

1 Like

Make it yourself.

you could make a vector3 face the tornado maybe. For that you could use CFrame.lookAt() and get that vector3

You can get the direction to send a part in by taking two parts position and subtracting them.

For example:
direction = (vector3Goal - vector3Start).Unit

you can then do:
part.AssemblyLinearVelocity = direction * velocity

This accomplishes what you wanted I believe. Keep in mind that this overrides its current velocity, if you want it to accelerate it you could instead slowly add velocity over time, this is just to give some direction. :slight_smile:

It looks like it just gets the direction to reach one position from another? You can do this with the following code:

local TornadoMoveSpeed = 5

local Direction = (TargetPosition - Tornado.Position).Unit
Tornado.Position += Direction * TornadoMoveSpeed

-- NOTE: Depending on how you have coded your tornado, this may cause the
tornado to move slower on laggier servers
1 Like

This works, thank you very much.

1 Like

Would instead of overriding the velocity adding the direction * velocity make it act like a lineforce?

I’m not using lineforces because it lags too much, using velocity like this is way faster.
This is not for the tornado, its for the debris.

It will just multiply your 2 velocities, it effectively gives you:
foo = (some vector3)
bar = (other vector3)
baz = Vector3.new(
foo.X * bar.X
…same for y and z

If you want to accelerate you can do it i over time with (for example) the heartbeat event and deltaTime, example:
heartbeat.Connect(function(deltaTime)
part.AssemblyLinearVelocity += accelerationVector3 * deltaTime
end)
This would accelerate it to have gained the entire accelerationVector3 within one second, just an example to show what I mean.
(deltaTime is the same as in Unity, if you keep adding deltaTime to 0 you will get 1 after exactly 1 second, it’s the some since the last heartbeat)

1 Like

so is accelerationVector3 the direction * velocity or what

Just for the record, only @dthecoolest’s suggestion to use Dot products would match the behavior of Project. The other solutions work great, but give you a vector pointing directly towards the goal rather than a projection.

local function Project(vector: Vector3, onNormal: Vector3)
  if onNormal.Magnitude < 0.001 then
    return Vector3.zero
  end
  local n = onNormal.Unit
  return n * n:Dot(vector)
end
4 Likes

If accelerationVector3 is direction it will in accelerate that way yes

1 Like

Yeah I guess project isnt the way to do this, Ill use @Shirovian’s answer

I can do this in parallel right?
I’m currently putting debris in a table and in a parallel heartbeat looping through it and putting the velocities in another table, then in the main loop, loop through that and set the velocity to that
Is this bad? (final question lol)

(just testing code dont think this is what my code actually looks like)

I’m not too brushed up on how parallel works but I think so yes, however with my previous experience with writing a parallel procedural terrain generator I think this might actually be less performant than if you just do it all non parallel, depends on how many parts you have. (you are just writing values, not doing any super slow calculating)
You can try and see if it’s necessary, but in general: yes.

Of course it lags, but I think it actually runs faster like this, thanks…

1 Like
1 Like

Just a side tip if you plan to have a big map, you could turn off CanTouch on the buildings parts to speed up performance a bit if you haven’t yet and don’t need the Touched events, good luck with your development!

1 Like

Yea, its gonna be a big map, but thats a good idea, I already turn off CastShadow, and turn it back on when it gets dropped

1 Like

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