Issue with RightVector

I have a script that iterates 10 times over, clones a part, and sets its position based on the character’s look vector.

This script also checks whether i is even or odd, and determines whether the part will be on the character’s left or right based on that.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local characterLookVector = character.HumanoidRootPart.CFrame.LookVector
local characterRightVector = character.HumanoidRootPart.CFrame.RightVector

local prevPart

for i= 1, 10 do
	local part = ReplicatedStorage.Part:Clone()
	
	if prevPart then
		part.Position = prevPart.Position + characterLookVector * 5
	else
		part.Position = character.Head.Position + characterLookVector * 20 - Vector3.new(0, 5, 0)
	end
	
	-- Confusion is here
	if i % 2 == 0 then
		part.Position -= characterRightVector * 30
	else
		part.Position += characterRightVector * 30
	end
	
	part.Parent = workspace
	part.Anchored = true
	part.Name = i
	
	-- Randomise orientation
	part.CFrame *=  CFrame.Angles(math.rad(math.random(1, 180)), math.rad(math.random(1, 180)), math.rad(math.random(1, 180)))
	
	prevPart = part
end

However, I am running into an issue here:

-- Confusion is here
	if i % 2 == 0 then
		part.Position -= characterRightVector * 30
	else
		part.Position += characterRightVector * 30
	end

The parts on the right are positioned as expected, however the parts on the left supposedly do not move?
image

Any help would be appreciated, thank you!

It’s because you’re basing the position off of the previous position. Instead of doing that, you should multiply the lookVector by i * 5

Thank you, this worked, however I am now facing another issue. When the user is a certain distance away from the world’s origin the parts are not properly positioned.


Any clue as to why this is? Thank you.

Can you send the updated script?

Sure thing

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

UserInputService.InputBegan:Connect(function(input)
	local characterLookVector = character.HumanoidRootPart.CFrame.LookVector
	local characterRightVector = character.HumanoidRootPart.CFrame.RightVector
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F then
			for i= 1, 10 do
				local part = ReplicatedStorage.Part:Clone()

				part.Position = characterLookVector * (i*5)

				if i % 2 == 0 then
					part.Position -= characterRightVector * 20
				else
					part.Position += characterRightVector * 20
				end

				part.Parent = workspace
				part.Anchored = true
				part.Name = i

				-- Randomise orientation
				part.CFrame *=  CFrame.Angles(math.rad(math.random(1, 180)), math.rad(math.random(1, 180)), math.rad(math.random(1, 180)))
			end
		end
	end
end)

Revise this line like so:

part.Position = character.HumanoidRootPart.Position + characterLookVector * (i*5)

Amazing thank you! I am relatively new to CFrame / Vector mathematics, could you kindly run me through the changes you’ve made and why these work?

Sure thing!
So the initial problem is that the parts on the right were in the correct position,

but because the position here is based off of the previous position rather than the center, the parts on the left were located at (position on the right - 30), or dead center.
Does that make sense?

The second problem is that LookVector and RightVector are both directional vectors rather than positions. By excluding the position, the parts always started at the origin of the world.
Does that also make sense? Feel free to ask for clarification or follow-up questions.

1 Like

Makes sense, thanks a lot for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.