How to apply a velocity only on the Z axis of object space

How to apply a velocity only on the Z axis of object space? If possible with AssemblyLinearVelocity

so, when I change the rotation the object will change the trajectory

1 Like

If the rocket is a BasePart or a Model with a primary part set, you can use CFrame.RightVector:

part.AssemblyLinearVelocity = part.CFrame.RightVector * desiredVelocity

@treebee63 LookVector should be used for Z axis, not RightVector. My bad :sweat_smile:

Do note that LookVector is negative Z axis though, if this is undesirable then ZVector should be used instead

1 Like
  1. (recommended way since it takes advantage of roblox’s physics engine) use LinearVelocity. There’s an option to switch its vector frame of reference from world to object (ie relative force).

  2. you can get the direction your model is pointing using its cframe and apply it to its AssemblyLinearVelocity.

local rocketSpeed = 100 -- studs/s

local cframe = part.CFrame
local front = cframe.LookVector
local up = cframe.UpVector
local right = cframe.RightVector

local back = -front
local left = -right
local bottom = -up

part.AssemblyLinearVelocity = up * rocketSpeed 
3 Likes

Yes, that is true.

Assuming you go the AssemblyLinearVelocity route, it seems like the author just wants the rocket to go where it’s pointed. 90% of the time if you’re not careful with the orientation of the object while 3d modelling or building it the true orientation could be pointing anywhere, including to the sides.

I’m assuming the author just labels it as the Z axis because some coordinate systems label Z as the vertical component and XY as horizontal. This does not translate to object space (we don’t know the object’s orientation in the picture). My hope is that the author will try all the vectors (up, down, left, right, foward, backwards) until the rocket goes where’s pointed.

2 Likes

That’s what I wanted, but when the rocket rotates (a slightly aggressive rotation) it just falls to the ground moments later. Maybe it’s because he starts speeding and then suddenly changes direction, thus losing speed?

oh, btw, the Z in world space is the X in the object space

If you’re using AssemblyLinearVelocity, you’ll need to update the property within a loop if you plan on rotating the rocket, otherwise the velocity will stay directed towards the rocket’s original value

1 Like

[Video in which the rotation is given first, waiting for it to rotate, and then adding the speed]

[Speeding and Rotating at same time]

Code:

local function missleActivateBooster(direction)

	
	local lv = currentMissile.CFrame.LookVector
	
	local bottom = -lv
	
	task.spawn(function()
		
		while currentMissile do
			
			task.wait()
			
			currentMissile.AssemblyLinearVelocity = lv * 500

		end
		
	end)
	
	task.wait(0.1)

end

ohhh am so dumb, forget about it sorry

Try rotating the rocket using AssemblyAngularVelocity

1 Like

Do you want the force to always go up or wherever it is facing?

Your rockets don’t seem to turn correctly right now. I made a little demo which combines both AlignOrientation and AssemblyLinearVelocity. You can tweak the values around until it moves the way you want it to.
Homing Missile Demo.rbxl (68.8 KB)

I tried combining AlignOrientation and LinearVelocity but it caused unwanted side effects.

--[[
	IMPORTANT NOTES:
	AlignOrientation
		- AlignType set to PrimaryAxisLookAt so that the constraint will force the missile to point at the target
		- Mode COULD be set to OneAttachment instead of 2 depending on the targetting system you use (while loop vs attachment based)
		
	Note: Avoid combining multiple constraints (ie linevector + align orientation) as their interaction may cause unwanted effects.
--]]

local missile = script.Parent
local function launch()
	task.defer(function()
		while task.wait() and missile.Parent do
			missile.AssemblyLinearVelocity = missile.CFrame.UpVector * 100
		end
	end)
	
	wait(2)
	missile.AlignOrientation.Attachment1 = workspace.Target.TargetAttachment
	missile.AlignOrientation.Enabled = true
end

task.wait(3)
launch()

Homing Missile Demo.rbxl (68.8 KB)

1 Like

I edited the script, and it is more or less this result, now there is another problem, as the video shows, I want the rocket to fall a little, then aim, and then it goes against the target, but when it finishes aiming and goes against the target, a very sudden change of speed direction happens, do you know any way to solve this?

I don’t know what control system you’re using so I cannot answer that question yet.

Are you using the constraint system (AlignOrientation, AlignPosition)? Are you using the legacy system (BodyPosition, BodyGyro)? Are you using a custom system?

Also could you reupload the video? It’s not playing in the post.

I see the script and the place you made, I just edited it

--[[
	IMPORTANT NOTES:
	AlignOrientation
		- AlignType set to PrimaryAxisLookAt so that the constraint will force the missile to point at the target
		- Mode COULD be set to OneAttachment instead of 2 depending on the targetting system you use (while loop vs attachment based)
		
	Note: Avoid combining multiple constraints (ie linevector + align orientation) as their interaction may cause unwanted effects.
--]]

local ts = game:GetService("TweenService")

local missile = script.Parent

task.spawn(function()
	
	while true do
		
		task.wait(0.1)
		print(missile.AssemblyLinearVelocity)
		
	end
	
end)

local function launch()
	
	missile.AssemblyLinearVelocity = missile.CFrame.UpVector * 400

	wait(3)
	missile.AlignOrientation.Attachment1 = workspace.Target.TargetAttachment
	missile.AlignOrientation.Enabled = true
	task.wait(0.5)	
	
	task.defer(function()
		while task.wait() and missile.Parent do
			missile.AssemblyLinearVelocity = missile.CFrame.UpVector * 100
		end
	end)
	
	
end

task.wait(3)
launch()

If you can’t play the video, let me know

The rocket looks fine in the video. I don’t understand your question, are you talking about the rocket speeding up when it turns towards the target and then slowing down once the AssemblyLinearVelocity section kicks in?

Or are you talking about gravity pulling the rocket back down? Once you disable the thrusters, the rocket will naturally drop down.

If you want the rocket to stay still in the air while it turns, you need to rapidly set the rocket’s velocity to 0.

local function launch()
	
	missile.AssemblyLinearVelocity = missile.CFrame.UpVector * 400

	wait(3)
	missile.AlignOrientation.Attachment1 = workspace.Target.TargetAttachment
	missile.AlignOrientation.Enabled = true

	task.defer(function() -- freezes the rocket in the air while it's turning
		while task.wait() and missile.Parent and missile.AlignOrientation.Enabled do
			missile.AssemblyLinearVelocity = Vector3.new(0,0,0)
		end
	end)
	task.wait(0.5)	
	
	task.defer(function()
		while task.wait() and missile.Parent do
			missile.AssemblyLinearVelocity = missile.CFrame.UpVector * 100
		end
	end)
	
	
end

I’m talking about the sudden change of direction. In real life, if a rocket is falling, it will take a few seconds to fall more and more slowly. After a while he will be able to climb at his maximum speed.