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

How do I create an area based Vector [?]

Asked by 5 years ago
Edited 5 years ago

Please include the code which you are trying to use, so the community will be better-equipped to help you with your problem.

Basically I've created a NPC follow script. I want to the NPC to move to any position within 3 studs away. I saw this before but I can't remember what it was.

My line: Humanoid:MoveTo(Position*Vector3.new(0,0,3)*Units)

My goal is to move the NPC anywhere 3 studs away from that position. I believe it had something to do with units but I can't remember.

Someone please refresh my memory.

Also explain how it works that would help alot.

PS: No one in the discord would help me :c

0
Why not just use the vector magnitude and have it stop when it is less than 3 units away? while (npc.Position - target.Position).Magnitude > 3.0 do wait(someTime) Humanoid:MoveTo(Position) end Cousin_Potato 129 — 5y
0
P.S. There's also the DistanceFromCharacter function if you don't want to use vector magnitude. https://developer.roblox.com/api-reference/function/Player/DistanceFromCharacter Cousin_Potato 129 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
local randomPosition = Vector3.new(math.random(-200, 200), math.random(-200, 200), math.random(-200, 200));
local normilizedRandomPosition = randomPosition/(math.abs(randomPosition.x) + math.abs(randomPosition.y) + math.abs(randomPosition.z));
Humanoid:MoveTo(Position + normilizedRandomPosition * 3);

This will move it three studs in any direction relative to its current position.

EDIT: Fixed code

EDIT: Explanation: The first line calculates a random distance. The second line takes that distance and normilizes it so that the absolute value of x, y, and z will add up to a total on one. The third line moves the humanoid to it's current position plus the position that was just normilzed multiplied by three; the outcome being that the model is moved three studs in any direction.

0
Would it work if I just did Position*3 BlackOrange3343 2676 — 5y
0
And why would I need to normalize it? BlackOrange3343 2676 — 5y
0
No, if you set Position to three times itself and it were equal to something like (100, 100, 100), then it would teleport 200 studs right, 200 studs up, and 200 studs forward. Normilzing makes it super easy to work with since you know it adds up to 1; which you can simply multiply by the number of studs you'd like it to travel. DM me on Discord if you'd like to ask me more about it. davidgingerich 603 — 5y
0
What's your discord? BlackOrange3343 2676 — 5y
View all comments (3 more)
0
And how come it's a random normalized position? Does it have to be random? What does normalizing do? BlackOrange3343 2676 — 5y
0
How does it affect the first vector value? BlackOrange3343 2676 — 5y
0
My Discord username is the same one I have on here. davidgingerich 603 — 5y
Ad

Answer this question