How To Make Homing Objects.: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Nate
m This is not an object either, it is a tutorial
>NXTBoy
Lots of rubbish here.
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{CatUp|Tutorials}}
__TOC__
==Rocket Propulsion And The Part==
==Rocket Propulsion And The Part==
Create the RocketPropulsion and the [[Part|part]] object.  Alternatively, you could make a missile with rocketpropulsion without a target, and put it in [[RBX.Lua.Lighting|Lighting]], then [[Clone|clone]] the rocket.
Create the RocketPropulsion and the [[RBX.lua.Part (Object)|part]] object.  Alternatively, you could make a missile with rocketpropulsion without a target, and put it in [[RBX.lua.Lighting (Object)|Lighting]], then [[Clone (Function)|clone]] the rocket.


==Defining the target==
===Defining the target===
This requires a [[Script|script]].  The script will define the target's [[Position|position]], the thing you want to home in on!
This requires a [[RBX.lua.Script (Object)|script]].  The script will define the target's [[Position (Property)|position]], the thing you want to home in on!


Example:
Example:
Line 10: Line 12:
</pre>
</pre>


==Firing The Part==
===Firing The Part===
This also requires a [[RBX.Lua.Script|script]], this will set off your part (If Unanchored) after The Target:
This also requires a [[RBX.lua.Script (Object)|script]], this will set off your part (If Unanchored) after The Target:


Example:
Example:
Line 18: Line 20:
</pre>
</pre>


==Additional Features==
===Additional Features===


The [[CartoonFactor]] property sets whether your part should head Directly towards it, ignoring the point its facing, or whether it faces straight at you.  1 Would be always facing at you.  0.5 Would be sort of facing you.  0 would be No effort to face you.
The [[CartoonFactor]] property sets whether your part should head Directly towards it, ignoring the point its facing, or whether it faces straight at you.  1 Would be always facing at you.  0.5 Would be sort of facing you.  0 would be no effort to face you.


:abort()
:[[Abort (Function)|abort()]]
This, as its name suggests, aborts the RocketPropulsion's mission to get to you, You could us this if your objects need fuel, and when the fuel is 0 they drop out of the sky.
This, as its name suggests, aborts the RocketPropulsion's mission to get to you, you could use this if your objects need fuel, and when the fuel is 0 they drop out of the sky.


==Connecting The Event==
===Connecting The Event===


You would need to do something like:
You would need to do something like:
script.Parent.Touched:connect(function(hit)
local a = hit.Parent and hit.Parent:findFirstChild("Humanoid")
if a ~= nil then
local rocket = game.Lighting.Rocket:clone()
rocket.Position = Vector3.new(0, 200, 0) -- drop out of sky
rocket.RocketPropulsion.Target = hit
rocket.Parent = game.Workspace
wait(0.01)
rocket.RocketPropulsion:fire()
end
end)
This is what you might use in some place:
a = Instance.new("RocketPropulsion")
a.Target = game.Players.PERSONNAME.Character.Torso
a.Parent = game.Players.Name.Character.Torso
== Another homing brick example ==
Insert objects into the workspace to make the following:
Part
  NumberValue { Name = "Speed", Value = 0.2 }
  BodyGyro
  BodyPosition
  Script
In the Script, insert the following:
<pre>
<pre>
function onTouched(hit)
local part = script.Parent
local a = hit.Parent.Humanoid
 
if a ~= nil then
function moveTo(target)
local b = game.Lighting.Rocket
local dir = (target.Position - part.Position).unit
b.Position = Vector3.new(0,200,0) -- drop out of sky
part.BodyGyro.cframe = CFrame.new(0, dir)
b.Parent = game.Workspace
part.BodyGyro.maxTorque = Vector3.new(9000, 9000, 9000)
b.RocketPropulsion.Target = hit
wait(0.01)
part.BodyPosition.position = target.Position
b.RocketPropulsion:fire()
part.BodyPosition.maxForce = Vector3.new(10000, 10000, 10000) * part.Speed.Value
end
 
function findNearestTorso(pos)
local nearest = {
torso = nil,
distance = math.inf
}
for _, object in ipairs(game.Workspace:GetChildren()) do
if object:isA("Model") and object ~= script.Parent then
local torso = object:findFirstChild("Torso")
local human = object:findFirstChild("Humanoid")
if torso and human and human.Health > 0 then
local this = {
torso = torso,
distance = (torso.Position - pos).magnitude
}
if this.distance < nearest.distance then
nearest = this
end
end
end
end
return nearest.torso, nearest.distance
end
end
while true do
local torso = findNearestTorso(part.Position)
if torso ~= nil then
moveTo(torso)
end
wait()
end
end
script.Parent.Touched:connect(onTouched)
</pre>
</pre>


==Using This Is Script Builder==
This will make a brick that will follow you.
This is what you might use in script builder:
 
<pre>
a = Instance.new("RocketPropulsion")
a.Target = game.Players.dreaddraco2.Character.Torso
a.Parent = game.Players.Name.Character.Torso
</pre>
This would make Name look like he's orbiting you.


==See also==
==See also==


*[[RBX.Lua.RocketPropulsion|Rocket Propulsion]]
*[[RBX.lua.RocketPropulsion (Object)|Rocket Propulsion]]
*[[Scripting]]
*[[Scripting]]


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 14:31, 16 September 2011

Rocket Propulsion And The Part

Create the RocketPropulsion and the part object. Alternatively, you could make a missile with rocketpropulsion without a target, and put it in Lighting, then clone the rocket.

Defining the target

This requires a script. The script will define the target's position, the thing you want to home in on!

Example:

game.Workspace.Part.RocketPropulsion.Target = game.Workspace.yourusernamehere.Head

Firing The Part

This also requires a script, this will set off your part (If Unanchored) after The Target:

Example:

game.Workspace.Part.RocketPropulsion:fire()

Additional Features

The CartoonFactor property sets whether your part should head Directly towards it, ignoring the point its facing, or whether it faces straight at you. 1 Would be always facing at you. 0.5 Would be sort of facing you. 0 would be no effort to face you.

abort()

This, as its name suggests, aborts the RocketPropulsion's mission to get to you, you could use this if your objects need fuel, and when the fuel is 0 they drop out of the sky.

Connecting The Event

You would need to do something like:

script.Parent.Touched:connect(function(hit)
	local a = hit.Parent and hit.Parent:findFirstChild("Humanoid")
	if a ~= nil then
		local rocket = game.Lighting.Rocket:clone()
		rocket.Position = Vector3.new(0, 200, 0) -- drop out of sky
		rocket.RocketPropulsion.Target = hit
		rocket.Parent = game.Workspace
		wait(0.01)
		rocket.RocketPropulsion:fire()
	end
end)

This is what you might use in some place:

a = Instance.new("RocketPropulsion") 
a.Target = game.Players.PERSONNAME.Character.Torso 
a.Parent = game.Players.Name.Character.Torso

Another homing brick example

Insert objects into the workspace to make the following:

Part
  NumberValue { Name = "Speed", Value = 0.2 }
  BodyGyro
  BodyPosition
  Script

In the Script, insert the following:

local part = script.Parent

function moveTo(target)
	local dir = (target.Position - part.Position).unit
	part.BodyGyro.cframe = CFrame.new(0, dir)
	part.BodyGyro.maxTorque = Vector3.new(9000, 9000, 9000)
	
	part.BodyPosition.position = target.Position
	part.BodyPosition.maxForce = Vector3.new(10000, 10000, 10000) * part.Speed.Value
end

function findNearestTorso(pos)
	local nearest = {
		torso = nil,
		distance = math.inf
	}
	
	for _, object in ipairs(game.Workspace:GetChildren()) do
		if object:isA("Model") and object ~= script.Parent then
			local torso = object:findFirstChild("Torso")
			local human = object:findFirstChild("Humanoid")
			if torso and human and human.Health > 0 then
				local this = {
					torso = torso,
					distance = (torso.Position - pos).magnitude
				}
				if this.distance < nearest.distance then
					nearest = this
				end
			end
		end
	end
	return nearest.torso, nearest.distance
end

while true do
	local torso = findNearestTorso(part.Position)
	if torso ~= nil then
		moveTo(torso)
	end
	wait()
end

This will make a brick that will follow you.

See also