How To Use BodyPosition: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
cleanup
>Quenty
No edit summary
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==Introduction==
==Introduction==
In this tutorial, you should be in '''ROBLOX STUDIO.'''
In this tutorial, you should be in [[Roblox Studio]].  Now, what may you ask, is a Body Position? Well, a body position is a way to position something using ROBLOX's physics.  The object will try to zoom to the point identified by the position, and will try using the amount of force defined.  Now this can be helpful.  What if you define that there is a lot of force on the Y axis (Up/Down), but not on a lot on the X/Z axis (Left/Right and Forward/Back).  In this case, you created a floating brick, that can move anywhere. This can be quite helpful in creating hover crafts or ships.  Another useful thing, is to remember that it will try to get to the position, but it won't necessarily get there. So if a brick that is anchored is in the way, that part probably won't get to the objective.


== Setup ==
== Setup ==
Alright, let's begin. First, make a small brick. I made mine 5x1x5.
First, make a small brick. I made mine 5x1x5.
Next, select the brick you just made and click Insert->Object->Script.
Next, select the brick you just made and click Insert->Object->Script.
Now double-click the new script object.
Now double-click the new script object.
== Code ==
You should see the following:
You should see the following:
<pre> print("Hello World!") </pre>
<pre> print("Hello World!") </pre>
Delete it, you won't need it.
Delete that text, you won't need it.
 
== Code ==


First, add this code.
First, add this code.
Line 18: Line 19:
<pre>local h = hit.Parent:findFirstChild("Humanoid")
<pre>local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then</pre>
if h ~= nil then</pre>
This will detect wether what touched it was a human or not. You don't want a rocket or slingshot to activate it do you?
This will detect whether what touched it was a human or not. You don't want a rocket or slingshot to activate it do you?
<pre>local b = Instance.new("BodyPosition")</pre>
<pre>local b = Instance.new("BodyPosition")</pre>
Ok, this defines our Bodyposition. But right now it doesn't do anything, because it doesn't have a parent.
Ok, this defines our Bodyposition. But right now it doesn't do anything, because it doesn't have a parent.
<pre>b.position = (500, 500, 500)
<pre>b.position = Vector3.new(500, 500, 500)
b.maxForce = (500000000, 500000000, 500000000)
b.maxForce = Vector3.new(500000000, 500000000, 500000000)
b.Parent = hit.Parent.Torso</pre>
b.Parent = hit.Parent.Torso</pre>
This will define some of BodyPosition's properties. I'll explain each one. BodyPosition's position property does this: When a BodyPosition has a parent, it will make the parent fly to the BodyPosition's position property. It's great for a lot of things, like teleporting without teleporting.
This will define some of BodyPosition's properties.  
Next we have the maxForce property. Simply put, it's how much force can be put on each axis of the parent brick. It also governs how far away the BodyPosition can be from it's position property before it will take a brick there.
*BodyPosition's position property does this: When a BodyPosition has a parent, it will make the parent fly to the BodyPosition's position property. It's great for a lot of things, like teleporting without teleporting.
Finally, the Parent of the BodyPosition is what the BodyPosition will be moving, in this case, the character's torso.
*maxForce property is how much force can be put on each axis of the parent brick. It also governs how far away the BodyPosition can be from it's position property before it will take a brick there.
*The Parent of the BodyPosition is what the BodyPosition will be moving, in this case, the character's torso.
 
So far we have this:
So far we have this:
<pre>function onTouched(hit)
<pre>function onTouched(hit)
Line 32: Line 35:
   if h ~= nil then
   if h ~= nil then
     local b = Instance.new("BodyPosition")
     local b = Instance.new("BodyPosition")
     b.position = (500, 500, 500)
     b.position = Vector3.new(500, 500, 500)
     b.maxForce = (500000000, 500000000, 500000000)
     b.maxForce = Vector3.new(500000000, 500000000, 500000000)
     b.Parent = hit.Parent.Torso</pre>
     b.Parent = hit.Parent.Torso</pre>
Alright, this is great. You'll fly to the point. But when will you drop? Thats a problem we have to fix. Let's try this:
 
So far, you'll fly to the point. But when will you drop? Thats a problem we have to fix. Let's try this:
<pre>wait(3)
<pre>wait(3)
b.Parent = nil</pre>
b.Parent = nil</pre>
Great! Now after 3 seconds, the BodyPosition ceases to exist, and you will drop! Now we just have to clean up the functions, and actually connect it to the Touched property!
 
Now, after 3 seconds, the BodyPosition ceases to exist, and you will drop! Now we just have to clean up the functions, and actually connect it to the Touched property!
 
<pre>  end
<pre>  end
end
end
Line 47: Line 53:
   if h ~= nil then
   if h ~= nil then
     local b = Instance.new("BodyPosition")
     local b = Instance.new("BodyPosition")
     b.position = (500, 500, 500)
     b.position = Vector3.new(500, 500, 500)
     b.maxForce = (500000000, 500000000, 500000000)
     b.maxForce = Vector3.new(500000000, 500000000, 500000000)
     b.Parent = hit.Parent.Torso
     b.Parent = hit.Parent.Torso
     wait(3)
     wait(3)
Line 60: Line 66:
== See Also ==
== See Also ==


[[BodyPosition]]
*[[BodyPosition]]
*[[Vector3]]
*[[Category:Scripting Tutorials]]

Latest revision as of 23:40, 26 April 2012

Introduction

In this tutorial, you should be in Roblox Studio. Now, what may you ask, is a Body Position? Well, a body position is a way to position something using ROBLOX's physics. The object will try to zoom to the point identified by the position, and will try using the amount of force defined. Now this can be helpful. What if you define that there is a lot of force on the Y axis (Up/Down), but not on a lot on the X/Z axis (Left/Right and Forward/Back). In this case, you created a floating brick, that can move anywhere. This can be quite helpful in creating hover crafts or ships. Another useful thing, is to remember that it will try to get to the position, but it won't necessarily get there. So if a brick that is anchored is in the way, that part probably won't get to the objective.

Setup

First, make a small brick. I made mine 5x1x5. Next, select the brick you just made and click Insert->Object->Script. Now double-click the new script object.

Code

You should see the following:

 print("Hello World!") 

Delete that text, you won't need it.

First, add this code.

 function onTouched(hit) 

This will detect when something touches it.

local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then

This will detect whether what touched it was a human or not. You don't want a rocket or slingshot to activate it do you?

local b = Instance.new("BodyPosition")

Ok, this defines our Bodyposition. But right now it doesn't do anything, because it doesn't have a parent.

b.position = Vector3.new(500, 500, 500)
b.maxForce = Vector3.new(500000000, 500000000, 500000000)
b.Parent = hit.Parent.Torso

This will define some of BodyPosition's properties.

  • BodyPosition's position property does this: When a BodyPosition has a parent, it will make the parent fly to the BodyPosition's position property. It's great for a lot of things, like teleporting without teleporting.
  • maxForce property is how much force can be put on each axis of the parent brick. It also governs how far away the BodyPosition can be from it's position property before it will take a brick there.
  • The Parent of the BodyPosition is what the BodyPosition will be moving, in this case, the character's torso.

So far we have this:

function onTouched(hit)
  local h = hit.Parent:findFirstChild("Humanoid")
  if h ~= nil then
    local b = Instance.new("BodyPosition")
    b.position = Vector3.new(500, 500, 500)
    b.maxForce = Vector3.new(500000000, 500000000, 500000000)
    b.Parent = hit.Parent.Torso

So far, you'll fly to the point. But when will you drop? Thats a problem we have to fix. Let's try this:

wait(3)
b.Parent = nil

Now, after 3 seconds, the BodyPosition ceases to exist, and you will drop! Now we just have to clean up the functions, and actually connect it to the Touched property!

  end
end
script.Parent.Touched:connect(onTouched)

There we go! those ends end the if statement and the function. Let's see the whole script.

function onTouched(hit)
  local h = hit.Parent:findFirstChild("Humanoid")
  if h ~= nil then
    local b = Instance.new("BodyPosition")
    b.position = Vector3.new(500, 500, 500)
    b.maxForce = Vector3.new(500000000, 500000000, 500000000)
    b.Parent = hit.Parent.Torso
    wait(3)
    b.Parent = nil
  end
end
script.Parent.Touched:connect(onTouched)

If you want to change where the character flies to, then you can change the BodyPosition's position property. Also, something really fun is if you only wait(0.1) then the character goes flying!

See Also