#Raycast Works On Object In Workspace But Does Not When Attached To Mouse Location

4 messages · Page 1 of 1 (latest)

frosty raven
#

I am building a tower defense game. So when building the tower would attach to the cursors location and perform collision checks.

The script below which changes color of its parent when it collides with another object. For context the tower would turn red if invalid and green if over a valid location. I tried 2 methods (ontouch event and raycasting). I found raycasting was better and more responsive so I went that route. The script would be attached to a part that is unanchored whereas the other part it interacted with would be anchored.

They both worked well but when attaching the tower directly to the mouse position this would break. Can someone explain why that is?

Here is the script:

local a = script.Parent
warn("Loaded ", a)
local isValid = a:WaitForChild("isValid")
local size = a.Size
local raycastResultLeft 
local raycastResultBottom
a.Color = Color3.new(0.317647, 1, 0)
while true do 

    raycastResultLeft = workspace:Raycast(Vector3.new(a.Position.x-a.Size.X/2, a.Position.y, a.Position.z+a.Size.Z/2), Vector3.new(0,0,-1.8))
    raycastResultBottom = workspace:Raycast(Vector3.new(a.Position.x-a.Size.X/2, a.Position.y, a.Position.z+a.Size.Z/2), Vector3.new(1.8,0,0))

    print(raycastResultLeft)
    print(raycastResultBottom)

    if raycastResultLeft == nil and raycastResultBottom == nil then
        a.Color = Color3.new(0.317647, 1, 0)
        isValid.Value = true
    else
        a.Color = Color3.new(1, 0, 0.0156863)
        isValid.Value = false
    end
    wait(0.1)
end

Ill post raycast script in next post as I reached character limit

#

and here is the script which attaches the object to the parent:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")
local TweenService = game:GetService("TweenService")
local GUI = script.Parent
local towerToSpawn = nil
local hoveredInstance = nil 
GUI.TowersToolbar.ExampleTower.Activated:Connect(function()
    AddPlaceHolderTower(t.Name)
end)

local function AddPlaceHolderTower(name)
    local towerExists = towers:FindFirstChild(name)
    if towerExists then
        -- Place Unit
        RemovePlaceHolderTower()
        towerToSpawn = towerExists:Clone()
        towerToSpawn.Parent = workspace
        -- set collision group
        for i, object in ipairs(towerToSpawn:GetDescendants()) do
            if object:IsA("BasePart") then
                if object.Name == "RangeIndicator" or object.Name == "UserCursorDecal" or object.Parent.Name == "UserCursorDecal" then
                    continue
                end
                object.CollisionGroup = MOB_COLLISION_GROUP
                object.Material = Enum.Material.ForceField
            end
        end
    end
end

--- every render frame update
RunService.RenderStepped:Connect(function()
    -- For larger decal use 5
    -- For smaller decal use 4 for larger and 2 for smaller bits
    local gridSize = 4/2
    local result = MouseRaycast({towerToSpawn})
    if result and result.Instance then 
        if towerToSpawn and towerToPlaceRef.Value ~= nil then
            hoveredInstance = nil
            -- show all decals
            local previewCollisions = workspace.PreviewCollisions:GetChildren()
            for i, v in ipairs(previewCollisions) do
                v.Transparency = 0.5
            end
            if result.Instance.Parent.Name == "TowerArea" then 
                canPlace = true
                SetColorPlaceHolderTower(Color3.new(0.101961, 1, 0.25098))
            else
                canPlace = false
                SetColorPlaceHolderTower(Color3.new(1, 0, 0.0156863))
            end
            local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 2)
            local x = math.round(result.Position.X/gridSize)*gridSize
            local z = math.round(result.Position.Z/gridSize)*gridSize

            local cframe = CFrame.new(x,y,z)
            towerToSpawn:PivotTo(cframe)
        else 
            -- no tower to spawn
            hoveredInstance = result.Instance
            local previewCollisions = workspace.PreviewCollisions:GetChildren()
            for i, decal in ipairs(previewCollisions) do
                decal.Transparency = 1
            end
        end
    else
        hoveredInstance = nil
    end
end)
#

Note: The tower attached to the mouse is under the "Checker" collision group where as towers themselves have a part object called Decal that it checks against.

frosty raven
#

Bump. Heres a video of the issue. Player model can walk and the colors will change. But when it interacts with a part it does not.