In the free model you have a C4 which you can place wherever you want and later detonate it by simply pressing a key. The issue I have is that when I try to place the C4 on a wall it obviously does not rotate accordingly and bugs into it.
What I want to achieve is that where ever I put my mouse at, the script can somehow detect the orientation of the mouse.Target and then rotate the C4 in an automated process approximately to it. I don’t need it to be perfect for spheres but when there’s a wall I would like to have it rotated 90 degrees.
This is the code used to determine the position of the C4 (LocalScript):
Edit: This is not the whole script but these are the necessary parts.
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local status = false
local tool = script.Parent
local function placingFire_Event()
local pos = mouse.Hit.p + Vector3.new(0,.4,0)
script.Parent.placeEvent:FireServer(pos)
status = true
end
tool.Activated:Connect(function()
local distChar = plr:DistanceFromCharacter(mouse.Hit.p)
if not hasDistLimit then
placingFire_Event()
return
end
if distChar <= distLimit then
placingFire_Event()
else
if plr.PlayerGui:FindFirstChild("overLimit") then return end
local uc = script.Parent.overLimit:Clone()
uc.Parent = plr.PlayerGui
uc.Enabled = true
wait(3)
uc:Destroy()
end
end)
This line is responsible for setting the C4s position and as already mentioned, I do not modify the orientation in any way because I just have no clue how to.
You should be able to get the object’s orientation with mouse.Target.Orientation. To match the orientation you could save it as a variable and apply it to the model.
Now it does indeed rotate but not properly.
As example I did not apply the mouse.Target.Orientation to the C4 tracking the cursor, just to the onces that get placed.
local function placingFire_Event()
local pos = mouse.Hit.p + Vector3.new(0,.4,0)
local orientation = mouse.Target.Orientation
print(orientation)
script.Parent.placeEvent:FireServer(pos, orientation)
status = true
end
Is there a documentation about this or something. I have used raycast before but not excessively enough to know a lot about it. How exactly would that work?
Hm so basically you can cast a unityray from your camera at the positions of your mouse, and then create a raycast using the unityray origin and direction in a 3D space
local unityray = camera:ScreenPointToRay(mouse.X, mouse.Y)
local raycast = workspace:Raycast(unityray.Origin, unityray.Direction * 100, params)
It rotates the part but not properly. And I printed out the orientation. Everything seemed correct.
Is there a chance this line causes the problem in the ServerScript. Can I just set the orientation of the C4 to the orientation of the wall or am I missing something?
part.Orientation = Vector3.new(orient)
-- part = the C4 being placed
-- orient = "raycast.Instance.Orientation"
Yeah because you have to construct a new cframe using the surface normal vector, the surface normal is a vector that points perpendicular to the surface
So I tried this out but to no avail. I got it to work on the wall but then it was bugging on the floor. It’s very late and in addition to this that exhausted me really. Perhaps if someone could provide me with the right calculations which I can then implement into my script, I would appreciate it.