How To Increase and Decrease Vector3 Values: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Chess123mate
I have redone the page. Code was being shown without knowledge of why it worked (tables) -- they are unnecessary here. Question -- why is declaring objects on this page???
>NXTBoy
Last section of this would probably be better moved to a page about object orientation. Do we have one of those?
Line 8: Line 8:
==Adding Vector3 Values==
==Adding Vector3 Values==


1) Insert a Vector3Value.<br>
# Insert a Vector3Value.
 
# Change the value of Vector3Value to 15, 15, 15.
2) Change the value of Vector3Value to 15, 15, 15.
# Click on Vector3Value.
 
# Insert a script. Make sure the script's parent is the Vector3Value.
3) Click on Vector3Value.
 
4) Insert a script. Make sure the script's parent is the Vector3Value.


== Scripting ==
== Scripting ==
Line 20: Line 17:
Once those parts are done, open the script. First, we need to identify the value we want to change. So put in this (note that ''local'' is not needed in a basic script, but ones that use functions may need it):
Once those parts are done, open the script. First, we need to identify the value we want to change. So put in this (note that ''local'' is not needed in a basic script, but ones that use functions may need it):


<pre>
local value = script.Parent.Value
local value = script.Parent.Value
</pre>


We now can tamper into the x, y, and z of the Vector3 value. Let's say we want to add 5 to the value along all three axes. There are two ways to do this. If there are more complicated calculations to be made, it will be better to do it this way:
We now can read the x, y, and z of the Vector3 value. Let's say we want to add 5 to the value along all three axes. There are two ways to do this. If there are more complicated calculations to be made, it will be better to do it this way:


<pre>
local x, y, z = value.x, value.y, value.z
local x = value.x
local y = value.y
local z = value.z
</pre>


Although we have the 3 seperate numbers, we still haven't added or subtracted them yet. In order to do that, you just have to add and subtract it like you do with regular numbers.
Although we have the 3 seperate numbers, we still haven't added or subtracted them yet. In order to do that, you just have to add and subtract it like you do with regular numbers.


<pre>
x, y, z = x + 5, y + 5, z + 5
x = x + 5
y = y + 5
z = z + 5
</pre>


Now we have the new x, y, and z. You could do extensive calculations on the x, y, and z variables, and you can treat them differently. You might multiply x by 2, for example, while leaving the others the same.  
Now we have the new x, y, and z. You could do extensive calculations on the x, y, and z variables, and you can treat them differently. You might multiply x by 2, for example, while leaving the others the same.  


<br><br> Note that although we have changed the values of the coordinates, we have not actually changed the Vector3Value. To do this, we need one final line:
Note that although we have changed the values of the coordinates, we have not actually changed the Vector3Value. To do this, we need one final line:


<pre>
script.Parent.Value = Vector3.new(x, y, z)
script.Parent.Value = Vector3.new(x,y,z)
</pre>


Let's take a look at what we have so far for this method:
Let's take a look at what we have so far for this method:


<pre>
local value = script.Parent.Value
local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z
local x = value.x
local y = value.y
x, y, z = x + 5, y + 5, z + 5
local z = value.z
x = x + 5
script.Parent.Value = Vector3.new(x, y, z)
y = y + 5
z = z + 5
</pre>


Now the value has changed. But how will we know? We just need to put in another line. Make sure you have output up first:
Now the value has changed. But how will we know? We just need to put in another line. Make sure you have output up first:


<pre>
print(script.Parent.Value)
print(script.Parent.Value)
</pre>


The script is complete! Let's just look at the finished script:
The script is complete! Let's just look at the finished script:


<pre>
local value = script.Parent.Value
local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z
local x = value.x
local y = value.y
x, y, z = x + 5, y + 5, z + 5
local z = value.z
x = x + 5
script.Parent.Value = Vector3.new(x, y, z)
y = y + 5
print(script.Parent.Value)
z = z + 5
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)
</pre>


Output should say 20, 20, 20. Each time you run the script, the coordinates will change again.
Output should say 20, 20, 20. Each time you run the script, the coordinates will change again.
Line 85: Line 61:
Usually there will be no need to keep track of all three individual coordinates. In this case, only one line is needed:
Usually there will be no need to keep track of all three individual coordinates. In this case, only one line is needed:


<pre>
script.Parent.Value = script.Parent.Value + Vector3.new(5, 5, 5)
script.Parent.Value=script.Parent.Value + Vector3.new(5,5,5)
</pre>
 
In this example, you change the Vector3.new(5,5,5) to anything you like, including negatives. This provides an alternative method to subtraction shown below.


In this example, you change the Vector3.new(5, 5, 5) to anything you like, including negatives. This provides an alternative method to subtraction shown below.


==Subtracting Vector3 Values==
==Subtracting Vector3 Values==




We've added them, but we now subtract them! Subtracting them is just like adding them, only we edit 3 lines.  
We've added them, but we now subtract them! Subtracting them is just like adding them, only we one line.  


<pre>
x, y, z = x - 5, y - 5, z - 5
x = x - 5
y = y - 5
z = z - 5
</pre>


Now, let's take a look at that edit in the full script:
Now, let's take a look at that edit in the full script:


<pre>
local value = script.Parent.Value
local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z
local x = value.x
local y = value.y
x, y, z = x - 5, y - 5, z - 5
local z = value.z
x = x - 5
script.Parent.Value = Vector3.new(x, y, z)
y = y - 5
print(script.Parent.Value)
z = z - 5
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)
</pre>


Output should say 10, 10, 10. You're done with subtracting them!
Output should say 10, 10, 10. You're done with subtracting them!
Line 123: Line 88:
This gets a bit more tricky to do.  Of course, you can always use the system used above, and get something like:
This gets a bit more tricky to do.  Of course, you can always use the system used above, and get something like:


<pre>
local value = script.Parent.Value
local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z
local x = value.x
local y = value.y
x, y, z = x*2, y*2, z*2
local z = value.z
 
x = x * 2
script.Parent.Value = Vector3.new(x,y,z)
y = y * 2
print(script.Parent.Value)
z = z * 2
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)
</pre>


...but sometimes one line is better to look at than 8.  So this time, we will multiply the Vector3 value itself instead of its individual values.
...but sometimes one line is better to look at than 8.  So this time, we will multiply the Vector3 value itself instead of its individual values.
Line 139: Line 100:
To do this, either multiply it by a number (x, y, and z will all be multiplied by this) or multiply it by another Vector3 value (x, y, and z from your original Vector3 will be multiplied with the respective number in the new Vector3).  A look at how this works:
To do this, either multiply it by a number (x, y, and z will all be multiplied by this) or multiply it by another Vector3 value (x, y, and z from your original Vector3 will be multiplied with the respective number in the new Vector3).  A look at how this works:


<pre>
local value = script.Parent.Value
local value = script.Parent.Value
print(value)  --Yes, you can print this way also.
print(value)  --Yes, you can print this way also.
script.Parent.Value = value * 2
script.Parent.Value = value * 2
print(script.Parent.Value)
print(script.Parent.Value)
</pre>


This script multiplies everything in the Vector3 value by the same number, which is 2 in this case.   
This script multiplies everything in the Vector3 value by the same number, which is 2 in this case.   
<br><br>
 
This is useful in several cases, such as vehicle tools.  But let's say we want to only multiply x and y by 2 and get rid of z, making it 0.  We could define each value separately and multiply it out like we did above, or we could do this:
This is useful in several cases, such as vehicle tools.  But let's say we want to only multiply x and y by 2 and get rid of z, making it 0.  We could define each value separately and multiply it out like we did above, or we could do this:


<pre>
print(script.Parent.Value)
print(script.Parent.Value)
local value = script.Parent.Value
local value = script.Parent.Value
value = value * Vector3.new(2, 2, 0)
value = value * Vector3.new(2, 2, 0)
print(value)
print(value)
</pre>


This script may seem a bit hard to comprehend at first, but the basic premise is this: it takes both x values and multiplies them, then both y values, then both z values.  The value becomes the result of each of those products; in this case, x and y are doubled and z is now 0.  Also, we can multiply the variable ("value") by a Vector3 and not have to worry about the script breaking.
This script may seem a bit hard to comprehend at first, but the basic premise is this: it takes both x values and multiplies them, then both y values, then both z values.  The value becomes the result of each of those products; in this case, x and y are doubled and z is now 0.  Also, we can multiply the variable ("value") by a Vector3 and not have to worry about the script breaking.
Line 161: Line 118:
Dividing also works the same way.  Just use / instead of *.
Dividing also works the same way.  Just use / instead of *.


<pre>
print(script.Parent.Value)
print(script.Parent.Value)
local value = script.Parent.Value
local value = script.Parent.Value
value = script.Parent.Value / 2
value = script.Parent.Value / 2
print(value)
print(value)
value = value / Vector3.new(2, 1, 0)
value = value / Vector3.new(2, 1, 0)
print(value)
print(value)
</pre>


Now test this script and... what's that?  It spits out INF for z (or NAN if it started as 0) at the end?  Well, that would be because a number was divided by 0.  Do try to avoid that. Remember that if you do not wish to change a coordinate, divide it by 1 to keep it the same.
Now test this script and... what's that?  It spits out INF for z (or NAN if it started as 0) at the end?  Well, that would be because a number was divided by 0.  Do try to avoid that. Remember that if you do not wish to change a coordinate, divide it by 1 to keep it the same.
Line 178: Line 133:


Your data type here will be a "Vector2"  
Your data type here will be a "Vector2"  
<pre>
Vector2 = {} --this declares an object of the class "Vector2" (yes, it is a table as well)


function Vector2.new(one, two) --the function that we will use to create new objects of this class  
Vector2 = {} --this declares the class "Vector2"
newTB = {x, y} --this makes our new object with the properties, which you set as objects in the table  
newTB.x = one --set the value to the one called in the argument  
function Vector2.new(one, two) --the function that we will use to create new objects of this class  
newTB.y = two  
    newTB = {x, y} --this makes our new object with the properties, which you set as objects in the table  
return newTB  
    newTB.x = one --set the value to the one called in the argument  
end  
    newTB.y = two  
</pre>
    return newTB  
end


Now, we can call a new object of the class as such:
Now, we can call a new object of the class as such:


<pre>
a = Vector2.new(7, 8)  
a = Vector2.new(7, 8)  
print(a.x) --> 7  
print(a.x) --> 7  
print(a.y) --> 8  
print(a.y) --> 8  
a.x = 10  
a.x = 10  
print(a.x) --> 10  
print(a.x) --> 10  
</pre>


== Adding member functions to your object ==  
== Adding member functions to your object ==  


<pre>
function Vector2.new(one, two)  
function Vector2.new(one, two)  
    newTB = {x, y}  
newTB = {x, y}  
    newTB.x = one  
newTB.x = one  
    newTB.y = two  
newTB.y = two  
   
 
    function newTB.reset()  
function newTB.reset()  
        newTB.x = 0  
newTB.x = 0  
        newTB.y = 0  
newTB.y = 0  
    end
end  
   
 
    return newTB  
return newTB  
end  
end  
 
a = Vector2.new(5, 8)  
a = Vector2.new(5, 8)  
 
print(a.x) --> 5  
print(a.x) --> 5  
print(a.y) --> 8  
print(a.y) --> 8  
 
a:reset()
a:reset()  
 
print(a.x) --> 0  
print(a.x) --> 0  
print(a.y) --> 0
print(a.y) --> 0 </pre>


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

Revision as of 11:11, 7 August 2011

Introduction

This tutorial will show you how to add or subtract Vector3 values. This will be quite a benefit to your scripts, and will make them more popular if you have them.

Adding Vector3 Values

  1. Insert a Vector3Value.
  2. Change the value of Vector3Value to 15, 15, 15.
  3. Click on Vector3Value.
  4. Insert a script. Make sure the script's parent is the Vector3Value.

Scripting

Once those parts are done, open the script. First, we need to identify the value we want to change. So put in this (note that local is not needed in a basic script, but ones that use functions may need it):

local value = script.Parent.Value

We now can read the x, y, and z of the Vector3 value. Let's say we want to add 5 to the value along all three axes. There are two ways to do this. If there are more complicated calculations to be made, it will be better to do it this way:

local x, y, z = value.x, value.y, value.z

Although we have the 3 seperate numbers, we still haven't added or subtracted them yet. In order to do that, you just have to add and subtract it like you do with regular numbers.

x, y, z = x + 5, y + 5, z + 5

Now we have the new x, y, and z. You could do extensive calculations on the x, y, and z variables, and you can treat them differently. You might multiply x by 2, for example, while leaving the others the same.

Note that although we have changed the values of the coordinates, we have not actually changed the Vector3Value. To do this, we need one final line:

script.Parent.Value = Vector3.new(x, y, z)

Let's take a look at what we have so far for this method:

local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z

x, y, z = x + 5, y + 5, z + 5

script.Parent.Value = Vector3.new(x, y, z)

Now the value has changed. But how will we know? We just need to put in another line. Make sure you have output up first:

print(script.Parent.Value)

The script is complete! Let's just look at the finished script:

local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z

x, y, z = x + 5, y + 5, z + 5

script.Parent.Value = Vector3.new(x, y, z)
print(script.Parent.Value)

Output should say 20, 20, 20. Each time you run the script, the coordinates will change again.

The Direct Method

Usually there will be no need to keep track of all three individual coordinates. In this case, only one line is needed:

script.Parent.Value = script.Parent.Value + Vector3.new(5, 5, 5)

In this example, you change the Vector3.new(5, 5, 5) to anything you like, including negatives. This provides an alternative method to subtraction shown below.

Subtracting Vector3 Values

We've added them, but we now subtract them! Subtracting them is just like adding them, only we one line.

x, y, z = x - 5, y - 5, z - 5

Now, let's take a look at that edit in the full script:

local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z

x, y, z = x - 5, y - 5, z - 5

script.Parent.Value = Vector3.new(x, y, z)
print(script.Parent.Value)

Output should say 10, 10, 10. You're done with subtracting them!

Multiplying/Dividing Vector3 Values

This gets a bit more tricky to do. Of course, you can always use the system used above, and get something like:

local value = script.Parent.Value
local x, y, z = value.x, value.y, value.z

x, y, z = x*2, y*2, z*2
script.Parent.Value = Vector3.new(x,y,z)
print(script.Parent.Value)

...but sometimes one line is better to look at than 8. So this time, we will multiply the Vector3 value itself instead of its individual values.

To do this, either multiply it by a number (x, y, and z will all be multiplied by this) or multiply it by another Vector3 value (x, y, and z from your original Vector3 will be multiplied with the respective number in the new Vector3). A look at how this works:

local value = script.Parent.Value
print(value)  --Yes, you can print this way also.
script.Parent.Value = value * 2
print(script.Parent.Value)

This script multiplies everything in the Vector3 value by the same number, which is 2 in this case.

This is useful in several cases, such as vehicle tools. But let's say we want to only multiply x and y by 2 and get rid of z, making it 0. We could define each value separately and multiply it out like we did above, or we could do this:

print(script.Parent.Value)
local value = script.Parent.Value
value = value * Vector3.new(2, 2, 0)
print(value)

This script may seem a bit hard to comprehend at first, but the basic premise is this: it takes both x values and multiplies them, then both y values, then both z values. The value becomes the result of each of those products; in this case, x and y are doubled and z is now 0. Also, we can multiply the variable ("value") by a Vector3 and not have to worry about the script breaking.

Dividing also works the same way. Just use / instead of *.

print(script.Parent.Value)
local value = script.Parent.Value
value = script.Parent.Value / 2
print(value)
value = value / Vector3.new(2, 1, 0)
print(value)

Now test this script and... what's that? It spits out INF for z (or NAN if it started as 0) at the end? Well, that would be because a number was divided by 0. Do try to avoid that. Remember that if you do not wish to change a coordinate, divide it by 1 to keep it the same.

Custom Objects

A custom object is something that you create that has properties like the other ROBLOX objects, such as game.Workspace.Brick.Transparency

Declaring an object

Your data type here will be a "Vector2"

Vector2 = {} --this declares the class "Vector2"

function Vector2.new(one, two) --the function that we will use to create new objects of this class 
    newTB = {x, y} --this makes our new object with the properties, which you set as objects in the  table 
    newTB.x = one --set the value to the one called in the argument 
    newTB.y = two 
    return newTB 
end

Now, we can call a new object of the class as such:

a = Vector2.new(7, 8) 
print(a.x) --> 7 
print(a.y) --> 8 
a.x = 10 
print(a.x) --> 10 

Adding member functions to your object

function Vector2.new(one, two) 
    newTB = {x, y} 
    newTB.x = one 
    newTB.y = two 
    
    function newTB.reset() 
        newTB.x = 0 
        newTB.y = 0 
    end
    
    return newTB 
end 

a = Vector2.new(5, 8) 

print(a.x) --> 5 
print(a.y) --> 8 

a:reset()

print(a.x) --> 0 
print(a.y) --> 0