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

How do i detect a change in a vector3?

Asked by
Echtic 128
5 years ago
Edited 5 years ago

So i need a camera.CFrame.LookVector to fire a function when changed.

Here's the code after adding what the second answer suggested:

wait()

local players = game:GetService("Players")
local player = players.LocalPlayer
local UserInputService = game:GetService("UserInputService")

local ev = workspace.Events.FloatFruitEvents.FloatingE
local ev2 = workspace.Events.FloatFruitEvents.FloatingoffE
local ev3 = workspace.Events.FloatFruitEvents.FloatingEv

local mouse = player:GetMouse()
local character = player.Character


UserInputService.InputBegan:Connect(function(key,gameProcessed)

    if(gameProcessed) then return end

    if key.KeyCode == Enum.KeyCode.F then 

local camera = workspace.CurrentCamera
local camcf = camera.CFrame

    ev:FireServer(camcf)


camera:GetPropertyChangedSignal("Orientation"):Connect(function()
    print("look vector changed")


    ev3:FireServer(camcf)


    end)
end

end)

UserInputService.InputEnded:Connect(function(key,gameProcessed) 

    if(gameProcessed) then return end

    if key.KeyCode == Enum.KeyCode.F then 


    ev2:FireServer()



end

end)

It's a local script.

0
did you try anything? if so can you post it Creacoz 210 — 5y
0
I have tried camera.CFrame.LookVector.Changed which doesn't work, then i tried camera.CFrame.LookVector.Value.Changed which i read from the developer wiki which doesn't work either. Echtic 128 — 5y

2 answers

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Edit

Turns out camera doesn't have a position/orientation property but camera's cframe does

local camera = workspace.CurrentCamera

currentLookVector = camera.CFrame.LookVector

camera:GetPropertyChangedSignal("CFrame"):Connect(function()
    if(camera.CFrame.LookVector ~= currentLookVector)then
        currentLookVector = camera.CFrame.LookVector
        print("look vector changed")
    end
end)

Previous answer

The code works for detecting the 'part' orientation, but camera doesn't have orientation property, so this wouldn't work for camera, see edit above for proper answer

With CFrame, the first parameter of CFrame.new() is the position and the second parameter, the lookVector, is the orientation. So to check when the orientation changes we are going to check when the orientation property changes:

I tested it on a part below just so you can see how it works, but I'll include the code that should work for your situation.

part = game.Workspace.Part
part:GetPropertyChangedSignal("Orientation"):Connect(function()
    print("look vector changed")
end)

--changing the look vector a bit just to test
wait(4)
part.CFrame = CFrame.new(part.Position, Vector3.new(4,4,4))
wait(4)
part.CFrame = CFrame.new(part.Position, Vector3.new(10,10,10))


With a camera, it would be

camera = game.Workspace.CurrentCamera

camera:GetPropertyChangedSignal("Orientation"):Connect(function()
    print("look vector changed")
end)

0
Says orientation is not a valid property name, i've tried swapping that with LookVector, CFrame, Position and even CFrame.LookVector and everything outputs the same exact error. Any other ideas? Echtic 128 — 5y
0
ill look into it in a few minutes for now, can i see what code you have? royaltoe 5144 — 5y
0
Alright, i edited the question, look into it when you have time. Echtic 128 — 5y
0
edited royaltoe 5144 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I am pretty sure you can use :GetPropertyChangedSignal() to get the Vector3 of something. Example:

local function onPosChange()
    -- fire or do anything here
end

part:GetPropertyChangedSignal("Position"):Connect(onPosChange)

I am not 100% sure if this works, but you should try it.

Hopefully it does help!

0
It's the same thing i said as a reply to the other answer Echtic 128 — 5y
0
but this one is an answer Creacoz 210 — 5y

Answer this question