How To Increase and Decrease Vector3 Values

From Goodblox Wiki
Jump to navigationJump to search

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 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:

local x = value.x
local y = value.y
local z = 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 = x + 5
y = y + 5
z = 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 = value.x
local y = value.y
local z = value.z
x = x + 5
y = y + 5
z = z + 5

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 = value.x
local y = value.y
local z = value.z
x = x + 5
y = y + 5
z = 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 edit 3 lines.

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

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

local value = script.Parent.Value
local x = value.x
local y = value.y
local z = value.z
x = x - 5
y = y - 5
z = 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 = value.x
local y = value.y
local z = value.z
x = x * 2
y = y * 2
z = 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 GoodBlox objects, such as game.Workspace.Brick.Transparency

Declaring an object

Your data type here will be a "Vector2"

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 
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