How To Make Homing Objects.: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>MrNicNac
Randomly capitalized word.
>NXTBoy
Lots of rubbish here.
 
(One intermediate revision by the same user not shown)
Line 30: Line 30:


You would need to do something like:
You would need to do something like:
<pre>
 
function onTouched(hit)
script.Parent.Touched:connect(function(hit)
local a = hit.Parent.Humanoid
local a = hit.Parent and hit.Parent:findFirstChild("Humanoid")
if a ~= nil then
if a ~= nil then
local b = game.Lighting.Rocket
local rocket = game.Lighting.Rocket:clone()
b.Position = Vector3.new(0,200,0) -- drop out of sky
rocket.Position = Vector3.new(0, 200, 0) -- drop out of sky
b.Parent = game.Workspace
rocket.RocketPropulsion.Target = hit
b.RocketPropulsion.Target = hit
rocket.Parent = game.Workspace
wait(0.01)
wait(0.01)
b.RocketPropulsion:fire()
rocket.RocketPropulsion:fire()
end
end
end
end)
script.Parent.Touched:connect(onTouched)
</pre>


This is what you might use in some place:
This is what you might use in some place:


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


== Another homing brick example ==
== Another homing brick example ==


* Insert > Object > Part<br>
Insert objects into the workspace to make the following:
* The name of this brick part is insignificant.<br>
 
* Insert > Object > NumberValue into the Part.<br>
Part
* Set NumberValue to 0.20000000000000001 and call it "Speed".<br>
  NumberValue { Name = "Speed", Value = 0.2 }
* Insert > Object > BodyGyro into the Part.<br>
  BodyGyro
* Insert > Object > BodyPosition into the Part.<br>
  BodyPosition
* Insert > Object > Script into the Part.<br>
  Script


In the Script, insert the following:
In the Script, insert the following:


<pre>
<pre>
bin = script.Parent
local part = script.Parent
 
function move(target)
local dir = (target.Position - bin.Position).unit
local spawnPos = bin.Position
local pos = spawnPos + (dir * 1)
bin:findFirstChild("BodyGyro").cframe = CFrame.new(pos,  pos + dir)
bin:findFirstChild("BodyGyro").maxTorque = Vector3.new(9000,9000,9000)
end


function moveTo(target)
function moveTo(target)
bin.BodyPosition.position = target.Position
local dir = (target.Position - part.Position).unit
bin.BodyPosition.maxForce = Vector3.new(10000,10000,10000) * bin.Speed.Value
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
end


function findNearestTorso(pos)
function findNearestTorso(pos)
local list = game.Workspace:GetChildren()
local nearest = {
local torso = nil
torso = nil,
local dist = 1000
distance = math.inf
local temp = nil
}
local human = nil
local temp2 = nil
for _, object in ipairs(game.Workspace:GetChildren()) do
for x = 1, #list do
if object:isA("Model") and object ~= script.Parent then
temp2 = list[x]
local torso = object:findFirstChild("Torso")
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
local human = object:findFirstChild("Humanoid")
temp = temp2:findFirstChild("Torso")
if torso and human and human.Health > 0 then
human = temp2:findFirstChild("Humanoid")
local this = {
if (temp ~= nil) and (human ~= nil) and (human.Health > 0then
torso = torso,
if (temp.Position - pos).magnitude < dist then
distance = (torso.Position - pos).magnitude
torso = temp
}
dist = (temp.Position - pos).magnitude
if this.distance < nearest.distance then
nearest = this
end
end
end
end
end
end
end
end
return torso
return nearest.torso, nearest.distance
end
 
function shoot(pos)
dir = (pos - bin.CFrame.p).unit
for i = 1, 50 do
local ex = Instance.new("Explosion")
ex.BlastRadius = 1
ex.Position = bin.Position + (dir * 10 * i) + (dir * 7)
ex.Parent = game.Workspace
end
end
 
function shootAt(torso)
local dir = (torso.Position - bin.Position).unit
local spawnPos = bin.Position
local pos = spawnPos + (dir * 1)
shoot(pos)
end
end


while true do
while true do
local torso = findNearestTorso(bin.Position)
local torso = findNearestTorso(part.Position)
if torso~=nil then
if torso ~= nil then
move(torso)
moveTo(torso)
moveTo(torso)
end
end
Line 132: Line 107:


This will make a brick that will follow you.
This will make a brick that will follow you.


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

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