Any help would be greatly appreciated!
I have a pet that follows the player, on the ground, but also rotates according to the normal of the surface, but at the same time still looks towards the player.
Currently there is an AlignPosition and an AlignOrientation in the pet that smoothly follows a part.
The way I’m currently doing this is by have the pet part, the part that the pet is aligned to, and a fakepart that rotates a lot to find the one rotation where it will be looking at the player.
Currently find this super inefficient since at least I’ll be doing one ray per frame and AT MOST it’ll be 37. Usually being around 10-25 rays a frame.
if p and pet and fakePart then
local ray = Ray.new((c.PrimaryPart.CFrame * CFrame.new(0,50,6.8)).p,Vector3.new(0,-500,0)) --first ray, to find placement behind player
local hit, pos, norm = workspace:FindPartOnRayWithIgnoreList(ray,{pet,p,fakePart})
if hit and pos and norm then
p.CFrame = CFrame.new(pos, pos+norm) * CFrame.Angles(math.rad(-90),math.rad(0),0) * CFrame.new(0,pet.Size.Y/2,0) --set according to surface normal then rotate to be upright then move up according to pet size
fakePart.CFrame = p.CFrame -- this is a fakePart that i will be rotation a maximum of 36 times (meaning 36 Ray casts that frame.
for i = 0,360,10 do -- rotate the fake part around to find the a rotation that is looking towards the player
local newRay = Ray.new(fakePart.Position,((fakePart.CFrame*CFrame.new(0,0,-50)).p-fakePart.Position).unit*15) --raycast from fakepart forward
local hit,pos,norm2 = workspace:FindPartOnRayWithIgnoreList(newRay,{pet,p,fakePart})
if hit and hit.Parent and hit.Parent.Name == player.Name then --if the hit is the in the character then set the part cframe to fakeparts cframe, which now inclues the surface normal rotation + w.e rotation was necessary to look at player
p.CFrame = fakePart.CFrame * CFrame.Angles(0,math.rad(10),0)
break
end
fakePart.CFrame = fakePart.CFrame * CFrame.Angles(0,math.rad(10),0)
end
end
end
Here are gifs that depict movement
Movement with fakepart to find orientation looking at player
Movement without finding orientation
I would love to not have to raycast so many times to get the desired affect, shown in the first gif.