Raycast not hitting all of the time

Good morning (or night (or evening (or afternoon (or noon whichever))))!

So, I’m recreating Geometry Dash, a 2D platformer/rhythm game, in Roblox.
I’ve got the movement of the cube in check (deactivated in the video shown later), and jumping is fine but it’s falling that’s going wrong.
HALF OF THE TIME, I land perfectly on the ground where I started the jump.
THE OTHER HALF OF THE TIME, I fall into the ground (usually 2 blocks down for some reason).

Here’s a video of that happening:
robloxapp-20240121-1407205.wmv (2.2 MB)

(For context, one block/unit is 2 studs.)
Here is the movement script (placed inside the yellow block):

local ts = game:GetService("TweenService")

local gameValues = game.ReplicatedStorage.Values

local speedValues = {
	slow = 8.6,     --0, Slow
	normal = 10.4,  --1, Normal
	fast = 12.96,   --2, Fast
	faster = 15.6,  --3, Faster
	fastest = 19.27 --4, Fastest
}

--TWEENS--
local jumpTween = ts:Create(script.Parent, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {
	CFrame = script.Parent.CFrame + Vector3.new(0, 4, 0)})
local fallTween = ts:Create(script.Parent, TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {
	CFrame = script.Parent.CFrame - Vector3.new(0, 4, 0)})

--GET ATTRIBUTES--
local currentSpeed = script.Parent:GetAttribute("Speed")

--MOVEMENT--
game:GetService("RunService").Heartbeat:Connect(function()
	--MOVEMENT--
	--script.Parent.Position += Vector3.new(0,0,(-0.05 * speedValues[currentSpeed]))
	
	--GROUND CHECKING--
	local raycast = workspace:Raycast(Vector3.new(script.Parent.Position.X, script.Parent.Position.Y - (script.Parent.Size.Y / 2) ,script.Parent.Position.Z), script.Parent.CFrame.LookVector * 0.25)
	
	if raycast then
		if raycast.Instance then
			script.Parent:SetAttribute("Grounded", true)
			jumpTween:Pause()
			fallTween:Pause()
		end
	end
	
	--JUMPING--
	if gameValues.Clicking.Value == true then
		if script.Parent:GetAttribute("Grounded") == true then
			script.Parent:SetAttribute("Grounded", false)
			jumpTween:Play()
			
			jumpTween.Completed:Connect(function()
				fallTween:Play()
			end)
		end
	end
end)

Thank you for any help you can give!

3 Likes

It looks like your forgetting to use raycastparams, but its a simple fix!

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = CHARACTER_GOES_HERE:GetDescendants()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true

local raycast = workspace:Raycast(Vector3.new(script.Parent.Position.X, script.Parent.Position.Y - (script.Parent.Size.Y / 2) ,script.Parent.Position.Z), script.Parent.CFrame.LookVector * 0.25, raycastParams)

Good luck my dude!

1 Like

(also I don’t recumbent using tweeting as jumping and falling)

1 Like

Even with the RaycastParams, it still clips into the floor.
Also, if I shouldn’t use tweens to make the player jump, how should I go about it while keeping the ease.

Just using Roblox in built gravity works fine in the falling stage and when you want the player to jump use a body force that stays inside the cube for around .1 seconds. That should give it a boost upwards. The easing happens automatically because the speed of an object accelerates to the ground by gravity in real life and in Roblox Studio.

This almost works, it’s just that the player bounces up and down when it falls (which it shouldn’t), and if the player falls from a great height, he will get misaligned and fall off the level (which it also shouldn’t).

This is a problem I get and here’s how I fix it:

  • Make a BodyVelocity and set the Velocity to 0,0,0. Then change the Max Velocity to 100000 on the axis you don’t want it to move and 0 on all other axis so for example 100000,0,0. – This locks the player in one axis
  • To fix the bounce issue you could play around with custom physical properties inside the characters parts (maybe the density) to fix the bouncing issue

You’ll just have to try stuff you will think will work if this doesn’t until something works, this is how I do it at least.

edit: You could also make a script that forces it into some axis

Here, for the direction vector (2nd arg), pass in this assuming you are trying to raycast downwards.

-Vector3.yAxis * 5 -- Change 5 to increase/decrease range

BodyVelocity is decrapritated (or however you say it) and they said to use VectorVelocity. VV doesn’t have a Max Velocity property, and eitherway I don’t think it would fix it because I think the unalignment issue is due to the bounce. Speaking of, there isn’t a density property inside a part.

This makes it so that if the cube isn’t holding jump, they stay in the air, and the player doesn’t fall down.

Which direction is the cube facing?

The direction of the cube is downwards. However, the cube is supposed to rotate 90 degrees when jumping (until the end of the jump) so this won’t work whenever I add that. In fact, that’s why the decal on it is rotated.

sorry for taking 22 hours to respond

Could you try raycasting towards this direction

-cube.CFrame.UpVector

I thought I sent this message but I clip a little bit into the floor, better than falling 2 blocks down though.

If I could find out how to instantly move the player out of the ground to lay flat on it, I guess it’d be good enough.

Try multiplying this with a scalar value to increase its range like such:

-cube.CFrame.UpVector * 3
1 Like

Even with a scalar value of 3, it still clips. I’ll try to increase the number, but I hope this doesn’t mean that sometimes, it will have inconsistencies.

Even with the number at 50 it still clips the same amount.

after 4 days i’ve come to the conclusion that: this is still not solved.

As well as I think that one frame, the player is moving down too fast and the raycast doesn’t hit and next frame, the cube is in the ground and the raycast hits then. I still don’t know how to make it hit but CFrame.UpVector * 50 still doesn’t hit.

Topic is kinda dead but doesn’t CFrame.UpVector * 50 change depending on the rotation?
And also why don’t you try raycasting a little bit more forward?

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = CHARACTER_GOES_HERE:GetDescendants()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true

local raycast = workspace:Raycast(Vector3.new(script.Parent.Position.X, script.Parent.Position.Y - (script.Parent.Size.Y / 2) ,script.Parent.Position.Z), script.Parent.Position - Vector3.new(0,script.Parent.Size.Y + .1,0), raycastParams)

Im not sure but this might solve your issue and to add on this raycasting is (from my experience) instant so it doesn’t lag behind