Part wont position to the ground even with raycast

I am trying to create a script that moves chickens to specific locations. I am doing this by using align position and orientation. I have also used a raycast to detect the ground and position the chicken to the ground however for some reason it doesnt seem to work.

This is the code:

local function positionChickens()
	-- create params
	local raycastParams =
	raycastParams.FilterDescendantsInstances = {character, chickenFolder}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	local characterCFrame, characterSize = character:GetBoundingBox()
	for i, chicken in pairs(chickenFolder:GetChildren()) do
		local chickenSize = chicken:GetExtentsSize()
		local distance = (chicken.PrimaryPart.Position - chicken.goal.Value.Position).Magnitude
		if distance <= 3 then
			if chicken.goal.Value == goal2.CFrame then
				chicken.goal.Value = goal2.CFrame
		local goalCF = chicken.goal.Value
		-- position pet to ground
		local ground = workspace:Raycast(goalCF.Position,,-(characterSize.Y * 3),0), raycastParams)
		if ground and ground.Instance then
			local newPos =, ground.Position.Y + (chickenSize.Y/2), goalCF.Position.Z)
			goalCF = * goalCF.Rotation
		local sin = (math.sin(15 * time() + 1.6 + chicken.offset.Value) / 0.5) + 1
		local cos = math.cos(7 * time() + 1+ chicken.offset.Value) / 4
		goalCF *=, sin, 0) * CFrame.fromEulerAnglesXYZ(0, 0, cos)
		chicken.PrimaryPart.AlignPosition.Position = goalCF.Position
		chicken.PrimaryPart.AlignOrientation.CFrame = * goalCF.Rotation


The current script in action:

Im not sure where I’m going wrong here. If anyone has any ideas it would be greatly appreciated.

1 Like

I don’t see how the code itself could be causing this. How are those numbers in your output related to the code? They look like goalCF.Position.Y. If they are, the problem might well be that rays are starting inside the base plate, which is thus not being hit.

For instance, if we cast a ray from Y 0.1 towards the base plate, it’s going to intersect it, but not if we cast from Y 0 or below.

Cute chickens :chicken:!

Those numbers are from the script that spawns the chickens, its their “offset” value used for making movement a bit more random.
I don’t believe its the raycast as an issue as It does detect there is a part below the chickens.

However, could it be something to do with align position?
The align position is pointing to a part in the workspace where I want the chickens to go, its X and Z coordinates dont get changed by the script but the Y ones do, due to the ground raycast and the hop created using the sin variable.
I’m thinking, is it not updating the chicken to go along the ground because its not exactly updating the position in real time? Its more just adjusting the future position of the chickens.

Good thinking, AlignPosition doesn’t react instantaneously. Nevertheless, it reacts to values that are set elsewhere.

Upon a closer look, I see the chickens all dive into the ground around the corner, where the goalCF changes.

I could try to recreate the whole situation, but there’s no guarantee that my version would completely match yours. It’s probably more reasonable that you debug by applying each change gradually.

  1. The raw goalCF

I haven’t noticed this before, but as long as chickenSize.Y remains unchanged, goalCF is constant after raycasting, because the origin is based on goalCF.Position, hence it’s constant for the given goal value. Is this intended, or did you mean to raycast from the chicken’s centre downwards? If so, also keep the above reply about intersections in mind.

  1. Sine offset
  • 2.1 Without offset.Value
  • 2.2 With offset.Value

Looks like the value is in range [-3, 3], but the chickens sink into the ground regardless of the offset.

Side notes
  • In line 37, the constructed CFrame is goalCF.
  • Each frame, character:GetBoundingBox() is acquired, and each chicken calls :GetExtentsSize().

It’s not clear what the character is (a chicken model; prototype for the clones?). Model extents don’t change with rotation, but the bounding box does. Evaluate if these methods have to be called all the time.

Ive tried removing the pet animation that takes sine into account, that didnt make any changes to them not going along the ground.
Also character variable is the Roblox players character, not the chickens.

I’m starting to think, possibly align position wont give me the intended outcome desired, and there may be a better way?
I also intend to further randomize the chickens movement such as making them stop, briefly walk in different directions and sometimes to run in a circle but whilst still making them move towards a goal part in the end. I’m not sure if align position would be the best thing for this?

What I’d kind of need is for the chickens position to be updated in real time to a set position whilst also being smooth, where the set position is always updating towards the final goal position.

You removed the impacts of sine, including offset, and they are still sinking? That stirrs some more confusion, because with the above code they are supposed to just float without fluctuation. There are only two unchanging goals after all.

Alright. I’m not entirely sure how it comes in play here in contrast to an arbitrary value, but the ray length isn’t affecting the outcome here.

That makes sense, albeit AlignPosition with appropriately tuned properties should, if anything, contribute to the smoothly and naturally perceived movement. Roblox solves physics at 240 Hz (unless adaptive time stepping is enabled).

You sure can try setting CFrames manually (at least for debugging).

If you still feel stuck and it’s not too much of a concern, you could try putting together a quick repo for us to debug (or just me in DMs, if you feel that would create too much exposure). I mean just strictly relevant code, and chickens reduced to bare cubes. I mentioned before that I can do my own version, but it probably won’t be the same (especially not AlignPosition properties).


@YT_GamingDan thank you, I’ll take a look the moment I can. I saved the place file, and I can delete it once we’re done.

1 Like

Here is the place available to download as a copy.

I am also going to try adjusting the chickens y coordinates separate to align position and see if that works.

Ive managed to get it working.

What I done was only update align position once, when the goal was changing, then using heartbeat I just adjusted the Y position of the chicken. I also noticed I was raycasting down from the goal part, not the chickens so I also changed that which means they will react to changing ground levels below them.

I appreciate your willingness to help with this, it has allowed me to further think through the solution to come up with a fix.

1 Like

Glad to read that!

Yeah, I was a slightly confused about the raycasting too (as long as chickenSize.Y remains unchanged, goalCF is constant after raycasting).

I’m looking forward to seeing your project come to life! :chicken:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.