Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a Humanoid move to a certain position but x amount of studs away from that position?

Asked by 4 years ago

So many people have told me to do something similar to:

Humanoid:MoveTo(Pos+Vector3.new(5,0,0))

Or

Humanoid:MoveTo(Pos+Vector3.new(0,0,5))

But the problem with this is that it will always be on the x or z axis. I just want the Humanoid to move to a position 5 studs away no matter the axis (preferably the closest possible).

So how would I do this? If you do need more information do comment.

0
possibly try using pythagorean identity theking48989987 2147 — 4y
0
I'm sorry, but I'm not really understanding this properly. Can you elaborate? Fifkee 2017 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Pythagorean Identity


basically, the pythagorean identity establishes that:

sin^2(?) + cos^2(?) = 1 (? being the angle measure / radians)

Which, if you, on a graphic calculator like desmos, were to graph

r = sin^2(?) + cos^2(?) (r standing for radius)

you would end up with a circle with radius 1

With that said, you could manipulate it so that you end up with a circle of any radius, for example, if you wanted a circle with radius a or radius 5, you could do:

r = asin^2(?) + acos^2(?)

r = 5sin^2(?) + 5cos^2(?)


Making the Code


local sPos = Vector3.new(1,0,2)--starting position

local function stud (theta)
    local pos = Vector3.new(5*math.sin(theta),0, 5*math.cos(theta))
        local finPos = sPos + pos

print((finPos - sPos).magnitude,"::",pos,"::",finPos,"::",math.deg(theta))
end

for i = 1, 360 do
    stud(math.rad(i))
end

Or more simplfied:

local humanoid = --humanoid object of your choice
local theta = math.rad(69)--for the memes
local sPos = Vector3.new(1,0,2)--some starting position
local pos = Vector3.new(5*math.sin(theta),0,5*math.cos(theta))
local finPos = sPos + pos --finished position

print((finPos - sPos).magnitude)--check distance
humanoid:MoveTo(finPos)

Note that I did not do 5*math.sin(theta)^2 or 5*math.cos(theta) in that script


explanation


This is probably the part of the answer I'm going to be lambasted for due to incorrect terminology,so i recommend skipping this part, if you already know the math behind it. But nevertheless, I digress. Put simply, this works, because, essentially, and point on a circle is reachable with a right triangle.

For a certain angle measure ?, the hypotenuse is the radius of the circle(r), as it reaches from the center to the edge of the circle. the vertical side would be r* sin(?) and the horizontal side would be r*cos(?).

This, on a circle with radius 1 would mean that the hypotenuse is 1, the vertical side is sin(?) and the horizontal side is cos(?), using pythagorean theorem gives us the famous equation of

sin^2(?) + cos^2(?) = 1


Useful Links


Here is the part of the answer where you see people explain the concept far better than i did

MathBits

Khan Academy

Hopefully this helped!

Ad
Log in to vote
0
Answered by 4 years ago

You would possibly be best using something like an Artificial Intelligence Script and then adjusting it so you can target what it moves. If you want to do it another way, I believe it would be quite hard due to having to calculate several positions.

Hope this helps.

Answer this question