[ ADVANCED ] GUI arrow won't follow a part in workspace?
Asked by
2 years ago Edited 2 years ago
Hello, my goal is self-explanatory, have a Ui arrow point in the direction of a part that is in the workspace.
The issue is that I cannot figure out how to make the Ui arrow move freely on the Frame, it sticks to the side of the Frame, making it look "janky" by constantly teleporting around the Frame from side to side.
Here is a video example of what I am trying to achieve and what I have so far.
imgur.com/a/MmHyoe4
https://imgur.com/a/MmHyoe4
And here is the LocalScript which makes my version work like it does.
002 | local Screen = script.Parent.Holder.AbsoluteSize |
003 | local T = game:GetService( "TweenService" ) |
005 | for i,v in pairs (game.ReplicatedStorage.Markers:GetChildren()) do |
006 | local Marker = script.Sample:Clone() |
007 | Marker.Parent = script.Parent.Holder |
008 | Marker.ImageColor 3 = v.Config.MarkerColor.Value |
009 | Marker.RotateLabel.Arrow.ImageColor 3 = v.Config.MarkerColor.Value |
010 | Marker.Icon.Image = v.Config.Icon.Value |
011 | table.insert(Markers, { Marker,v } ) |
014 | function ClampMarkerToBorder(X,Y,Absolute) |
018 | local DistanceToXBorder = math.min(X,Screen.X-X) |
019 | local DistanceToYBorder = math.min(Y,Screen.Y-Y) |
021 | if DistanceToYBorder < DistanceToXBorder then |
022 | if Y < (Screen.Y-Y) then |
023 | return math.clamp(X, 0 ,Screen.X-Absolute.X), 0 |
025 | return math.clamp(X, 0 ,Screen.X-Absolute.X),Screen.Y - Absolute.Y |
028 | if X < (Screen.X-X) then |
029 | return 0 ,math.clamp(Y, 0 ,Screen.Y-Absolute.Y) |
031 | return Screen.X - Absolute.X,math.clamp(Y, 0 ,Screen.Y-Absolute.Y) |
036 | game:GetService( "RunService" ).Heartbeat:Connect( function () |
037 | for i,Stat in pairs (Markers) do |
038 | local Marker = Stat [ 1 ] |
039 | local Location = Stat [ 2 ] |
040 | Marker.Visible = Location.Config.Enabled.Value |
042 | if Location.Config.Enabled.Value then |
043 | local MarkerPosition , MarkerVisible = game.Workspace.CurrentCamera:WorldToScreenPoint(Location.Position) |
044 | local MarkerRotation = game.Workspace.CurrentCamera.CFrame:Inverse()*Location.CFrame |
045 | local MarkerAbsolute = Marker.AbsoluteSize |
047 | local MarkerPositionX = MarkerPosition.X - MarkerAbsolute.X/ 2 |
048 | local MarkerPositionY = MarkerPosition.Y - MarkerAbsolute.Y/ 2 |
050 | if MarkerPosition.Z < 0 then |
051 | MarkerPositionX,MarkerPositionY = ClampMarkerToBorder(MarkerPositionX,MarkerPositionY,MarkerAbsolute) |
053 | if MarkerPositionX < 0 then |
055 | elseif MarkerPositionX > (Screen.X - MarkerAbsolute.X) then |
056 | MarkerPositionX = Screen.X - MarkerAbsolute.X |
058 | if MarkerPositionY < 0 then |
060 | elseif MarkerPositionY > (Screen.Y - MarkerAbsolute.Y) then |
061 | MarkerPositionY = Screen.Y - MarkerAbsolute.Y |
065 | Marker.RotateLabel.Visible = not MarkerVisible |
066 | Marker.Visible = not MarkerVisible |
067 | Marker.RotateLabel.Rotation = 90 + math.deg(math.atan 2 (MarkerRotation.Z,MarkerRotation.X)) |
068 | Marker.Position = UDim 2. new( 0 ,MarkerPositionX, 0 ,MarkerPositionY) |
070 | T:Create(Marker,TweenInfo.new(. 5 ), { ImageColor 3 = Location.Config.MarkerColor.Value } ):Play() |
071 | T:Create(Marker.RotateLabel.Arrow,TweenInfo.new(. 5 ), { ImageColor 3 = Location.Config.MarkerColor.Value } ):Play() |
072 | local CurrentTime = tick() |
073 | if Location.Config.BurstType.Value = = "Normal" then |
074 | if math.floor((CurrentTime% 1.5 )* 100 )/ 100 < = . 01 then |
075 | local Burst = script.BurstRings:Clone() |
076 | Burst.ImageColor 3 = Location.Config.MarkerColor.Value |
077 | Burst.Parent = Marker |
078 | T:Create(Burst,TweenInfo.new( 1 ), { Size = UDim 2. new( 2 , 0 , 2 , 0 ),ImageTransparency = 1 } ):Play() |
079 | game.Debris:AddItem(Burst, 1 ) |
081 | elseif Location.Config.BurstType.Value = = "Police" then |
082 | if math.floor((CurrentTime% 1.5 )* 100 )/ 100 < = . 01 then |
084 | local Burst = script.BurstRings:Clone() |
085 | Burst.ImageColor 3 = Color 3. fromRGB( 82 , 124 , 174 ) |
086 | Burst.Parent = Marker |
087 | T:Create(Burst,TweenInfo.new( 1 ), { Size = UDim 2. new( 2 , 0 , 2 , 0 ),ImageTransparency = 1 } ):Play() |
088 | game.Debris:AddItem(Burst, 1 ) |
089 | elseif math.floor((CurrentTime%. 75 )* 100 )/ 100 < = . 01 then |
091 | local Burst = script.BurstRings:Clone() |
092 | Burst.ImageColor 3 = Color 3. fromRGB( 255 , 89 , 89 ) |
093 | Burst.Parent = Marker |
094 | T:Create(Burst,TweenInfo.new( 1 ), { Size = UDim 2. new( 2 , 0 , 2 , 0 ),ImageTransparency = 1 } ):Play() |
095 | game.Debris:AddItem(Burst, 1 ) |