Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I rotate this projectile?

My script is as follows...

-- Set Function 

local Rocket = Instance.new('Part') do
	-- Set up the rocket part
	Rocket.Name = 'Rocket'
	Rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	Rocket.Size = ROCKET_PART_SIZE
	Rocket.Orientation = CFrame.Angles.new(0,0,math.rad(180))
	
Rocket.CanCollide = false

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', Rocket)
	mesh.MeshId = MISSILE_MESH_ID
	mesh.Scale = MISSILE_MESH_SCALE
	
	-- Add a force to counteract gravity
	local bodyForce = Instance.new('BodyForce', Rocket)
	bodyForce.Name = 'Antigravity'
	bodyForce.force = Vector3.new(0, Rocket:GetMass() * GRAVITY_ACCELERATION, 0)


When the script is executed the projectile is fired backwards out of the Rocket Launcher. I know that in order to determine the Rotation I put


Rocket.Orientation = Vector3.new(???,??? * ???, ???) -- This is where I encounter problems.

Thanks for your consideration!

The Bottom Line: How to call rotation functions and how to determine rotation using Vector3.

Thanks

EDIT

Ok SO basically I have added the CFrame to the Projectile, but the mesh itself will not rotate. I cannot figure out how to change it

Here is the script:

	-- Create a clone of Rocket and set its color
		local rocketClone = Rocket:Clone()
		--game.Debris:AddItem(rocketClone, 30)
		rocketClone.BrickColor = player.TeamColor
		rocketClone.Touched:connect(function(hit)
			if hit and hit.Parent and hit.Parent ~= player.Character and hit.Parent ~= tool then
				rocketClone:Destroy()
			end
		end)
		
		spawn(function()
			wait(30)
			if rocketClone then rocketClone:Destroy() end
		end)		
		
		-- Position the rocket clone and launch!
		local spawnPosition = (tool.Handle.CFrame * CFrame.new(2, 0, 0)).p
		rocketClone.CFrame = CFrame.new(spawnPosition, target)* CFrame.Angles(0 ,0 ,0) --NOTE: This must be done before assigning Parent
		rocketClone.Velocity = rocketClone.CFrame.lookVector * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = game.Workspace		
		
		-- Attach creator tags to the rocket early on
		local creatorTag = Instance.new('ObjectValue', rocketClone)
		creatorTag.Value = player
		creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
		local iconTag = Instance.new('StringValue', creatorTag)
		iconTag.Value = tool.TextureId
		iconTag.Name = 'icon'
		
		delay(attackCooldown, function()
			canFire = true

17 comments

0

You should use CFrame to set the parts position and rotation

0

How would I go about doing that?

0

as orientation takes into account collisions

0

Cframe.Angles

0

I have looked at this article. What would I put here --> "CFrame rotation = CFrame.new(--->)

0

Maybe (Rocket.x,Rocket.y*180,Rocket.z)

0

Change the position.

0

keep in mind that CFrame.Angles and the fromeuleangles functions all use radians , not degrees

0

Hmm ok

0

How do I set radians? Sorry

0

CFrame.Angles(math.rad(),0,0) use math.rad(90) for rotation in radians

0

CFrame.Angles(math.rad(),0,0) use math.rad(90) for rotation in radians

0

Cool Thanks for the help... I'll reply after I attempt to rotate the mesh

0

@ExHydraboy can you also edit your question to include the attempt.

0

Definitely

0

Updated

1 answer

In most cases you will just be setting the parts CFrame as this contains both the position and rotation. If you are using Orientation then it needs to be a Vector3 and not a CFrame.

For this task you should be using CFrame. As this is a rocket launcher the starting point of the rocket will be at the rocket launcher CFrame.

local Rocket = Instance.new('Part')

Rocket.Name = 'Rocket'
Rocket.FormFactor = Enum.FormFactor.Custom 
Rocket.Size = ROCKET_PART_SIZE
Rocket.CFrame = RocketLauncher.CFrame 
Rocket.Parent = workspace

The next stage is to include an offset to position the pocket at the end of the RocketLauncher. For this we will add an offset in object space as the RocketLauncher is facing the direction that the rocket is shooting we just need to add onto this facing direction to get to the end of the RocketLauncher.

local Rocket = Instance.new('Part')

Rocket.Name = 'Rocket'
Rocket.FormFactor = Enum.FormFactor.Custom 
Rocket.Size = ROCKET_PART_SIZE
Rocket.CFrame = RocketLauncher.CFrame * CFrame.new(0, 0, 5) -- move foward 5 studs check CFrame math operators 
Rocket.Parent = workspace

The last stage now that the Rocket is in the correct position is to include the correct roation. This is done by using CFrame.Angles with the correct rotation in radians.

local Rocket = Instance.new('Part')

Rocket.Name = 'Rocket'
Rocket.FormFactor = Enum.FormFactor.Custom 
Rocket.Size = ROCKET_PART_SIZE
Rocket.CFrame = RocketLauncher.CFrame
	 * CFrame.new(0, 0, 5) -- position offset
	 * CFrame.Angles(0, 0, math.rad(180)) -- new rotation
Rocket.Parent = workspace

You might need to do some testing to get the correct rotation.

I hope this helps. please comment if you have any other questions.

6 comments

0

Thank You so Much!

0

I guess I should look a bit more into how to use CFrames LOL

0

no problem. CFrame logic can be a bit difficult to understand at first but check the wiki page and look up global and object space.

0

I found a problem with the script I am editing... The CFrames for the Rocket are located BELOW the instance function

0

It's a clone function BUUUt I may have figured it out

0

At this stage I would make another question explaining the new problem and include screenshots.

This discussion is preserved as it was. New answers, comments, and votes are closed.