generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

[Need Help] 2D Mouse Position to 3D World Position.

Previous Thread :: Next Thread 
xxmobkiller is not online. xxmobkiller
Joined: 23 Nov 2015
Total Posts: 3
25 Jan 2016 03:36 PM
I'm trying to make a script so when I click on a Part in side a model like.
Model/Parts-Model/Part Then if my mouse is over a Part in side Model it will look for ItemID if it founds it then it will get hitbox at Model/Hitbox and set it to the current selected so when I right click it whilst I am hover on that hitbox it will select that model and update the location whilst my mouse is moving but I only want it to snap to the grid with like min Y is 5 it so it will all ways stay at 5 and it will check to see if the hitbox is colliding with other object hitbox if so then it turn the current SelectionBox to Really Red but when I move the model it will check again to see if Its hitting other Hitbox if not then it will let me Right click placing the object permently. I tryied to code this but I got so far with my game that I can't let this one problem stop me from making my game. I have listed the code I got now it works but not what I want it to do it does not Check if its hitting other hitbox or stay at Y 5 and it keeps staying under the camera even when I zoom out it stays in the right bottom conor :( Here is the code I got I know my coding sucks but its works but I'm needing a help with a better code. I tried google and Roblox Forums nothing did not help. so thats why I posted this and here is the code.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --Getting the player's mouse
local selection = Instance.new("SelectionBox", player.PlayerGui)
selection.Color = BrickColor.new("Really blue")
selected = nil
moveable = false
clickedOnce = false
clickedTwice = false

mouse.Move:connect(function()
local target = mouse.Target
if not target and clickedOnce == false then
moveable = false
selection.Adornee = nil
selected = nil
else
if clickedOnce == false and target.Name ~= "Terrain" and target.Parent:FindFirstChild("ItemId") or
clickedOnce == false and target.Name ~= "Terrain" and target.Parent.Parent:FindFirstChild("ItemId") or
clickedOnce == false and target.Name ~= "Terrain" and target.Parent.Parent.Parent:FindFirstChild("ItemId") or
clickedOnce == false and target.Name ~= "Terrain" and target.Parent.Parent.Parent.Parent:FindFirstChild("ItemId") then
if target.Parent:FindFirstChild("Hitbox") then
selection.Adornee = target.Parent.Hitbox
selected = target.Parent.Hitbox
moveable = true
elseif target.Parent.Parent:FindFirstChild("Hitbox") then
selection.Adornee = target.Parent.Parent.Hitbox
selected = target.Parent.Parent.Hitbox
moveable = true
elseif target.Parent.Parent.Parent:FindFirstChild("Hitbox") then
selection.Adornee = target.Parent.Parent.Parent.Hitbox
selected = target.Parent.Parent.Parent.Hitbox
moveable = true
elseif target.Parent.Parent.Parent.Parent:FindFirstChild("Hitbox") then
selection.Adornee = target.Parent.Parent.Parent.Parent.Hitbox
selected = target.Parent.Parent.Parent.Parent.Hitbox
moveable = true
else
selection.Adornee = nil
selected = nil
end
elseif clickedOnce == false then
selection.Adornee = nil
selected = nil
end
end

if clickedOnce == true and clickedTwice == false then
if selection.Adornee ~= nil and selected ~= nil then
local item = selection.Adornee
local hitPart, hitPos = ScreenToWorld()
if hitPart ~= nil and hitPos ~= nil then
item.Parent:MoveTo(Vector3.new(hitPos.X,item.Position.Y,hitPos.Z))
--print("HitPart = " .. hitPart.Name .. " HitPos = " .. hitPos.X)
end
end
end

end)

mouse.Button1Down:connect(function()
if clickedOnce == false and clickedTwice == false then
clickedOnce = true
elseif clickedOnce == true and clickedTwice == false then
clickedTwice = true
selection.Adornee = nil
selected = nil
clickedOnce = false
clickedTwice = false
end
end)

local RayCastIgnoreList = workspace.FindPartOnRayWithIgnoreList
local function Raycast(ray, ignoreNonCollidable, ignoreList)
local ignoreList = ignoreList or {}
local hitPart, hitPos = RayCastIgnoreList(workspace, ray, ignoreList)
if hitPart then
--if ignoreNonCollidable and hitPart.CanCollide == false then
--table.insert(ignoreList, hitPart)
--return Raycast(ray, ignoreNonCollidable, ignoreList)
--end
return hitPart, hitPos
end
return nil, nil
end

function ScreenToWorld()
local camera = game.Workspace.CurrentCamera
local unitRay = camera:ScreenPointToRay(camera.ViewportSize.X, camera.ViewportSize.Y)
local ray = Ray.new(unitRay.Origin, unitRay.Direction*400)
local hitPart, hitPos = Raycast(ray, true, {game.Players.LocalPlayer,game.Workspace.RainbowScannerUpgrader})

if hitPart ~= nil and hitPos ~= nil then
return hitPart, hitPos
end

return nil, nil
end
Report Abuse
xxmobkiller is not online. xxmobkiller
Joined: 23 Nov 2015
Total Posts: 3
28 Jan 2016 12:16 AM
Bumped
Report Abuse
AdvancedObjects is not online. AdvancedObjects
Joined: 11 Dec 2012
Total Posts: 3354
28 Jan 2016 12:21 AM
The great wall of text.



#Code print("Song Link: http://www.roblox.com/Deorro-vs-Swedish-House-Mafia-Save-The-5-Hours-item?id=340827217")
Report Abuse
mycheeze is not online. mycheeze
Joined: 27 Jun 2011
Total Posts: 6748
28 Jan 2016 12:25 AM
This great wall of text puts Trump's plan to shame
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image