AlignPosition ignoring set position and snapping target character to origin

hello! i’m trying to replace a grab system in my game based on WeldConstraints with AlignPosition. currently when I position the characters to each other (using OneAttachment mode) the alignposition seems to ignore the position I set and instead position them near the center of the world. How it’s intended to work is so the enemy character is held in place right in front of the character as the attack animation plays and damages them.

here’s the code I use to make the alignposition

function Welder.AlignCharacters(firstCharacter : Model, secondCharacter : Model, atPosition : Vector3): AlignPosition
    local secondCharacterRootAttachment = findFirstDescendant(secondCharacter, "RootAttachment")
    
    local alignPosition = Instance.new('AlignPosition')
    alignPosition.Parent = secondCharacterRootAttachment
    alignPosition.RigidityEnabled = true 
    alignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
    alignPosition.Attachment0 = secondCharacterRootAttachment
    alignPosition.Position = atPosition
    alignPosition.MaxForce = 100000
    alignPosition.Responsiveness = 200
    
    alignPosition.Enabled = true 
    
    
    return alignPosition
end

https://i.gyazo.com/897d6efe0d1e621fe162eab90e978910.mp4

Perhaps the issue lies here:

local firstCharacterRootAttachment = findFirstDescendant(firstCharacter, "RootAttachment")
alignPosition.Attachment0 = firstCharacterRootAttachment

Hi, secondCharacter in this instance is supposed to be the enemy character.

Interestingly enough, when I set the 3rd arg to be humanoidRootPart.Position, this is how the alignPosition functions.

External Media External Media
newHitbox.HitSomeone:Connect(function(hitCharacters)
		for index, enemyCharacterModel in hitCharacters do
			local humanoidRootPart : BasePart = characterWCS.Instance:FindFirstChild("HumanoidRootPart")
			
			local enemyCharacterWCS = CharacterClass.GetCharacterFromInstance(enemyCharacterModel)
			local enemyRootPart : BasePart = enemyCharacterModel:FindFirstChild("HumanoidRootPart")
			
			if enemyCharacterWCS:HasStatusEffects({RagdollStun}) then continue end
						
			local randomGrabSound = GrabSoundsFolder:GetChildren()[math.random(1, #GrabSoundsFolder:GetChildren())]
			local soundPlayer = SoundManager.new(characterWCS.Instance, randomGrabSound)
			soundPlayer:Start(Players:GetPlayers())
			

			local alignPosition = Welder.AlignCharacters(enemyCharacterModel, humanoidRootPart.Position + Vector3.new(0, 0, -5))
			
			clinchTrack:GetMarkerReachedSignal("hitboxEnd"):Once(function()
				alignPosition:Destroy()
			end)
		end
	end)

This is where I call the alignPosition. Note that I took out the firstCharacter argument since I realized it wasn’t doing anything

I would take its CFrame relative to the humanoid root part’s look vector by doing this operation, this should work both globally and locally:

local alignPosition = Welder.AlignCharacters(enemyCharacterModel, humanoidRootPart.CFrame:PointToWorldSpace(Vector3.new(0,0,-5)))

Are there any other main issues that I am missing?

1 Like

This works! I also added this small modification to the code so the enemy doesn’t get stuck inside the user. CFrames are still a mystery to me…

local alignPosition = Welder.AlignCharacters(enemyCharacterModel, humanoidRootPart.CFrame:PointToWorldSpace(Vector3.new(0,0,-7)))			
			
			local updateConnection = RunService.Stepped:Connect(function()
				alignPosition.Position = humanoidRootPart.CFrame:PointToWorldSpace(Vector3.new(0,0,-7))
			end)
			
			clinchTrack:GetMarkerReachedSignal("hitboxEnd"):Once(function()
				updateConnection:Disconnect()
				alignPosition:Destroy()
			end)
1 Like

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