Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I create an area based Vector [?]

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

2 comments

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

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

1 answer

✓ Accepted 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.

7 comments

0

Would it work if I just did Position*3

0

And why would I need to normalize it?

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.

0

What's your discord?

0

And how come it's a random normalized position? Does it have to be random? What does normalizing do?

0

How does it affect the first vector value?

0

My Discord username is the same one I have on here.

This discussion is preserved as it was. New answers, comments, and votes are closed.