Can't orientate part to face a position

Hello, I can’t seem to figure out how to align my “Footprint” part with the ground and make it point to the next waypoint.

Skjermbilde 2021-08-19 180443
A is supposed to point towards B while staying aligned with the ground

Here’s what I got so far:

function animalModule.createFootprints(animal, waypoints)
	local footstepFrequency = 5
	
	local count = 1
	for i, wp in pairs(waypoints) do
		count = count +1
		if count == footstepFrequency+1 then count = 1 else continue end
		
		
		local rayOrigin = wp.Position
		local rayParams = RaycastParams.new()
		if animal.char then	
			rayParams.FilterDescendantsInstances = {animal.char}
			rayParams.FilterType = Enum.RaycastFilterType.Blacklist
		end
		
		
		
		local rayDirection = Vector3.new(0,-5,0)
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rayParams)
		if raycastResult and i+1 < #waypoints then
			local mat = raycastResult.Material
			if mat == Enum.Material.Grass or mat == Enum.Material.LeafyGrass or mat == Enum.Material.Mud  then
				local footstep = Instance.new("Part", workspace.Footprints)
				footstep.Size = Vector3.new(4,4,.1)
				footstep.Transparency = 1
				footstep.CanCollide = false
				footstep.Anchored = true
				footstep.Name = "Footprint"
				footstep.CFrame = CFrame.lookAt(raycastResult.Position, raycastResult.Position + raycastResult.Normal)

				local decal = Instance.new("Decal", footstep)
				decal.Face = Enum.NormalId.Front
				decal.Texture = "rbxassetid://"..animal.footprintIds[math.random(1,#animal.footprintIds)]

				game:GetService("Debris"):AddItem(footstep, animalModule.footprintLifetime)

			end
		end	 
	end
end

I’m only creating a Footprint for every 5 waypoints, but I want each Footprint to point towards the next waypoint.

Any help would be appreciated! :grinning:

You can probably use the surface normal of the raycast along with from matrix to do it

local dir = (nextPrint-pos).Unit
CFrame.fromMatrix(pos,dir,norm)

If it’s 90 degrees sideways just do

dir:Cross(norm) 

instead of just dir

If it’s backwards just make it negative

1 Like
local footstep = Instance.new("Part", workspace.Footprints)
footstep.Size = Vector3.new(4,4,.1)
footstep.Transparency = 1
footstep.CanCollide = false
footstep.Anchored = true
footstep.Name = "Footprint"
				
local dir = (waypoints[i + 1].Position - raycastResult.Position).Unit
dir:Cross(raycastResult.Normal)
footstep.CFrame = CFrame.fromMatrix(raycastResult.Position, dir, raycastResult.Normal)

I’m not sure if this is what you meant, but this is what i got:
Skjermbilde 2021-08-19 183737

Try

CFrame.fromMatrix(pos, dir:Cross(norm), dir)

1 Like

Thank you! It worked, I also had to resize the part :laughing:

1 Like