Hello, I want to use Raycasting in my game to see if the player is <1 studs above the ground, but I really don’t how to do raycasting lol. Could someone please teach me?
If you want to use a raycast to find the distance to the ground, you can do it with workspace:Raycast().
The workspace:Raycast() function takes 3 parameters, the origin position, the direction it fires in, and an optional RaycastParams object for saying it should ignore certain object for example, it’s worth noting that the distance the ray goes before giving up is determined by the magnitude of the direction vector, so (0,1,0) would only go up 1 stud before giving up after not hitting anything, and longer raycasts take longer to compute.
It is also worth noting that if you started the raycast from the position of the HumanoidRootPart, it would be >1 stud even if you were standing on the ground, since your legs would put your HumanoidRootPart 2 studs above the ground, to fix this, you can just add the HipHeight of the Humanoid to the distance check (HipHeight represents how high off the ground the characters torso is)
If a raycast doesn’t hit anything, the workspace:Raycast() function just returns nil.
In your case, the script you’re looking for probably looks something like:
local params = RaycastParams.new()
params.FilterDescendantsInstances = {character} --A table of objects whos descendants will be ignored in the raycast
local ray = workspace:Raycast(character.HumanoidRootPart.Position, Vector3.new(0, -1, 0)*(character.Humanoid.HipHeight+1), params) --We say *(HipHeight+1) because we want to check 1 past the HipHeight
if ray then --If the ray exists, as in, if it's not nil
--The ray hit something so clearly the character is on the ground
end
But in you’re case, and, correct me if I’m wrong, I’m guessing you’re trying to use this to check if the player is on the ground? In which case, you can use the Humanoid.StateChanged event to detect whenever the character changes state (Starts climbing, stops climbing, starts swimming, jumps, falls, lands, etc), and the Humanoid:GetState() to determine what the state is currently.
If you’re trying to check if the humanoid is on the ground, you’d want to look for the Enum.HumanoidStateType.Running state, or if you’re using the Humanoid.StateChanged event to detect when they reach the ground, you’d want to check if the state at the time of the event call is Enum.HumanoidStateType.Landed, like so:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() --If player.Character is nil, as in, it hasn't been created yet, wait until it is.
local humanoid = character:WaitForChild("Humanoid") --Wait until Humanoid is created inside the character.
humanoid.StateChanged:Connect(function(state) --Event passes the state it just changed to.
if state == Enum.HumanoidStateType.Landed then --Landed means it just hit the ground after falling.
print("Humanoid just landed on the ground!")
end
end)
while true do wait(1)
if humanoid:GetState() == Enum.HumanoidStateType.Running then --Running means it's on the ground, not necessairily that it's running on the ground, just that it's on the ground.
print("Humanoid is on the ground as we speak!")
end
end
Hope this helps!
I used Humanoid.StateChanged and it didn’t really work. It would remove my BodyVelocity even if I wasn’t on the ground, which I don’t want to happen.
Really? That’s strange, did you use an if statement to check if the state just changed to Enum.HumanoidStateType.Landed? Or did you just remove the BodyVelocity after any state change, that’s the only way I can imagine that happening.
Yes I did;
if state == Enum.HumanoidStateType.Landed or state == Enum.HumanoidStateType.Physics then
I added .Physics because .Landed wasn’t working sometimes.
Heres the whole part of it. I created a BodyVelocity and changed the Velocity to high numbers.
if state == Enum.HumanoidStateType.Landed or state == Enum.HumanoidStateType.Physics then
VF.Force = Vector3.new(0,0,0)
VF:Destroy()
elseif state == Enum.HumanoidStateType.Freefall or state == Enum.HumanoidStateType.Flying then
VF.Force = Vector3.new(-10000,-20000,-30000);
Is the BodyForce created inside or outside of the StateChanged event? If it’s inside I think the problem may be that it only checks the state for that specific BodyForce once, if it’s outside, well frankly I have no idea.
I’m not sure what you mean by inside or outside, but the entire code I posted in my previous post about the StateChanged function is in an entire function:
player.Character:WaitForChild("Humanoid").StateChanged:Connect(function(_, state)
if state == Enum.HumanoidStateType.Landed or state == Enum.HumanoidStateType.Physics then
VF.Force = Vector3.new(0,0,0)
VF:Destroy()
elseif state == Enum.HumanoidStateType.Freefall or state == Enum.HumanoidStateType.Flying then
VF.Force = Vector3.new(-10000,-20000,-30000);
end
end)
Could it be that you create the BodyForce inside of the character only once, load into the game, and then fall to the ground because you spawn in the air, triggering the event to delete your bodyforce, and then you jump and it does nothing because the BodyForce is already gone? That’s my best guess, I’m somewhat at a loss here.
I don’t blame you lol, also no. I spawn on a tower I built.
The whole game is about a spiderman swinging effect and the BodyForce is what gives the player the “swing”.
It might be possible that simply spawning on the platform is changing your state to landed or physics, perhaps if you put different print statements in both the landed and freefall checks and see which one gets printed first we can find out some helpful information.
Will do. However it’s kind of late for me right now and school tomorrow and such. Got to sleep. I will do this first thing tomorrow though after school and keep you posted.
Back from school. Also, StateChanged somehow works now??? I really don’t know. It may have been something to do with the forces since I changed them around a bit. The prints are printing in the right place now.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.