What do you want to achieve?
I am basically making a game based on building in a short period of time. In first person a player is supposed to select a brick and then drag it around and then place it where he wants. So if the player rotates the brick rotates with him and if he looks up it will follow. However, I do not wish for it to be welded. I want it to still have the same rotation.
What is the issue?
The issue is that I have no idea on how to achieve this task. I can barely explain it even.
What solutions have you tried so far?
Unfortunately, I did not find any solutions to this and for some reason I cannot find a way to replicate what I want.
I already created the system of the mouse selecting the part. However, as said above I have no idea how to make it move.
It would be greatly appreciated if someone could help me or at the very least pin point me in the right direction.
Thank you so much.
You are basically asking for how to make the part follow the mouse.
The Player’s Mouse object has a property called Hit, which is essentially the position. of the mouse in 3D space
Every frame, if an object is being placed, you can update the CFrame of the object to Mouse.Hit, which will move it to the mouse’s location in 3D space.
If you want the initial rotation of the part to be preserved, you’ll have to construct a CFrame that has the same rotation as the object’s initial CFrame, but a different position when you move it to the mouse.
There is a guide on the devforum about a similar system, but it’s a bit more complex than your use case. However, it will be helpful when you want to implement features like collision checking, so the player can’t overlap multiple parts, or saving the parts placed. It’s worth looking at, if not there have been a multitude of guides published on this sort of system.
This requires some knowledge of how CFrame works, and binding all of the logic to RenderStepped or Heartbeat.
Right, I understand. However, my main question was how could I make it so the part would be lets say 5 studs away from the player every time i move it.
I don’t quite understand what you are asking, so let me answer to both questions I think you are asking.
If you are trying to make the part move every time the player moves, as oppose to every time the player moves the mouse, you would need to run a script that checks the players position while they have the part selected, I’d recommend using p.HumanoidRootPart.Changed(a), where a is the attribute being changed, checking it with if a == "Position" then. If the position is changed then you could update the parts position relative to the players position.
If you are trying to make the part move with the players mouse, then the answer @Shardwielder gave should work perfectly for all intensive purposes, where-as you update the part’s position to Mouse.Hit so long as the part is selected.
If neither of these are answers to what you are trying to do, please give a more detailed description of what you are trying to accomplish.
If I understand you correctly, you can get the camera’s LookVector from its CFrame, and then multiply it by how many studs you want it away from the camera, then add it to the camera’s position, and I believe that should be the Vector3 position of where you want the part to be. You would just have to update that every frame.
e.g. (in localscript, as Camera is local)
brick.CFrame = camera.CFrame * CFrame.new(0, 0, -5) – try 5 if its wrong, use the X,Y to change it as well.
So that this applies every frame, you should use RenderStepped as it will update it every frame (well prior to every frame but y’know).
To make it smoother (as that will move it instantly), you should use :lerp so it interpolates smoothly.
The player is in first person, and I want the part move with the mouse. However, as said mouse.Hit would not be suitable for me as I do not want the character to place the part far away from him if it makes sense. I want the part to be dragged with the player as said 10 studs away. SO basically it is not like any of the placement systems, it is rather a place it where you want, on the ground, levitate it etc without it rotating. I don’t know if this makes sense but the video I provided shows what I mean
Right, I think that makes sense, how would the script look like?
My current script I got from a person kind of is right but it rotates with it which I do not want.
local function getCFrame()
local headCF = player.Character.Head.CFrame
local mouseT = mouse.Target
if (mouseT) then
local lookCF = CFrame.new(headCF.p, mouseT.CFrame.p) -- Head CF looking at mouse
local mag = (headCF.p - mouseT.CFrame.p).Magnitude -- Distance between mouse & head
local CF = lookCF * CFrame.new(0, 0, -(mag/2)) -- Exact middle between mouse CFrame and head CFrame
return CF
end
end
If you want something like the system provided then you would use something like:
if (Mouse.Hit - player.HumanoidRootPart.Position).magnitude <= 10 then
-- code stuff
end
this will check if the mouse’s position is under or equal to 10 studs away so that the player can not place anything more than 10 studs away from them. Not sure how much this is helping as I am not entirely sure on what you are asking.
If you want it to move as far away as it can, but no more than say 5 studs. Then you should cast a ray to find the closest part directly in line with the camera, and set the CFrame to that part, or if its too far limit it to say 20 studs. https://www.robloxdev.com/api-reference/function/Workspace/FindPartOnRay
I personally don’t see why you would raycast for a part placement, if I am understanding him correctly, which I guess I might not be; he wants the part to move with the mouse but he doesn’t want it to move more than 10 studs away from the character, which could be done by establishing a constant magnitude value and not running the code if the magnitude value of the Mouse.Hit and the player’s humanoidrootpart exceeds the constant.
Yes, that’s right. So the player selects a part. Lets say the player move right the part moves with the player and the rotation stays the same. The player unselects the part and the part then stays at the position. I do not want a placement system if you get me now.
Yeah; if you are aware of how to script it, then it would go something like this;
Mouse.Changed:Connect(function(name)
if name == "Hit" then
if (Mouse.Hit - player.HumanoidRootPart).magnitude <= 10 then
-- script stuff
end
end
end)
It’d also be useful to establish a magnitude between the part and the 4 walls surrounding the area so that a part doesn’t partially clip through, or completely vanish through a wall; though you could accomplish this with raycasting as well.
You are right, however my game that I am planning to make isn’t the typical furniture placement system. It is supposed to get messy in a way so it would not be that much of a problem. Like in the video there would just be parts that a player can pick up and build.
The rotation theoretically wouldn’t change because the mouse can only be render in one rotational cframe value (to my knowledge atleast), which would result in the part being moved in only one rotation, so you wouldn’t need to worry about the parts rotation being changed or anything.