Hi, I am trying to move a mesh with my code for my farming system. I have a piece of dirt in ServerStorage that gets cloned (the tilled land that you plant crops on). I am trying to move this to the players HumanoidRootPart position.
The problem with just changing the position is that if I am on a hill and I do something like this:
local function plant()
local clone = placingDirt
local hrp = tool.Parent.HumanoidRootPart
clone.Parent = game.Workspace.Spawn.Farming
clone.Position = (hrp.Position.X, 0, hrp.Position.Z) --Not that this would work just an example
end
This results in the tilled land going inside the ground at Y: 0.
local veryhighnumber = 100000
local verylownumber = -100000
local rayOrigin = vector3.new(hrp.Position.X, veryhighnumber, hrp.Position.Z)
local rayDestination = vector3.new(hrp.Position.X, verylownumber, hrp.Position.Z)
local rayDirection = rayDestination - rayOrigin
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
if raycastResult then
clone.Position = raycastResult.Position
clone.Orientation = raycastResult.Normal -- imagine assuming the ground is always flat, couldn't be us
end
I never tested my script, tell me if it works.
also consider adding
Uh like any number you want really, I am just subtracting the vector 3 to get a direction because for some reason, in the past if I just do like vector3.new(0,-1,0) or something for ray direction, it doesn’t work for me.
I am assuming math.huge won’t work but you can try, like I said, I haven’t tested my code, and could not right now.
It seems like the subtraction would give you (0, -200,000, 0) because you are subtracting the x and the y with the x and the y so they cancel out. Also -100,000 - 100,000 would equal -200,000. Should I just do:
you can try that, but make sure you do vector3.new, and also make sure the y value is not zero since the subtracting find the distance between the vector from how I understand it…
That’s going to be a problem, because raycasts have a maximum distance of 5000 studs. Also, you should always make your raycasts as short as you can, since it limits the worst-case performance by not unnecessarily checking for collisions across your entire world…
Orientation is set from a Vector3 of Euler angles. The result.Normal vector is a unit vector that is the direction of the surface normal, which is not angles, just a direction. So this line of code would (if the ground is flat) give your part a 1-radian rotation about the Y-axis, which is not going to be what you expected.
Depending on how simple this game is, you may be able to avoid the raycast entirely by getting the Y position from one of the following:
Directly from whatever part is already at the player’s feet (e.g. a baseplate)
Just placing the item below the hrp.Position.Y by (0.5 * hrp.Size.Y + Humanoid.HipHeight + 0.5 * dirt.Size.Y) (if character is standing)
If the ground height is the same everywhere, you could just hardcode it too.
Yes. I didn’t mean you’d be able to simply copy Position.Y, I meant that you may be able to calculate the position you need from a part that you know is part of the ground, rather than having to do a raycast. Like if you had a baseplate. You still need to account for the thickness of the parts.
yea raycast was a bad idea since in Humanoid.FloorMaterial you can literally avoid the problem of trying to till while jumping and the part appear in the air…
I got blinded by raycast since I recently was doing raycast lol
Ok so now I have the the distance from the players HumanoidRootPart to the ground (2.195), and a way not till the ground while jumping (thanks @phenix_rider1), but how do I actually teleport the mesh?
My current code:
local function plant()
local clone = placingDirt
local hrp = tool.Parent.HumanoidRootPart
local hum = hrp.Parent.Humanoid
clone.Parent = game.Workspace.Spawn.Farming
if hum.FloorMaterial == "Grass" then
--teleport stuff here
end
end