How to Tween A Character’s Position to Another Character Whislt They Keep Facing Them

I’m making a turn-based game and I’m a little new and a bit of a goofball at CFrame and Tweening (also a bit of a goofball posting on this forum, I messed up the first post). Simply, I’m trying to make it so when a character does a basic attack, they move toward the enemy they are attacking whilst still facing them. Here is a snippit of code:

function globalAnims.dashForw(model, enemModel)
    local charHRP = model:FindFirstChild("HumanoidRootPart")
    local enemHRP = enemModel:FindFirstChild("HumanoidRootPart")
    --Initially trying to make it face the enemy player wants to target immediately
    charHRP.CFrame = CFrame.lookAt(charHRP.Position, enemHRP.Position)

    local charTweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0)
    --Initially thinking it would just set the position to the enemy's HRP but it also rotates it
    local properties = {CFrame = CFrame.new(enemHRP.Position)}

    local tween = TS:Create(charHRP, charTweenInfo, properties)

    tween:Play()
end

So it starts like this:


However after the tween plays the character slowly rotates and ends up like this:
image

I’ve done some things such as:
Trying to only set Position in the tweenInfo (didn’t work, made character spin around like he was having a seizure)
Trying to change orientation through CFrame.Angles instead of CFrame.lookAt() (i messed up alot so i gave up)

Thank you in advanced!

1 Like
local properties = {CFrame = CFrame.new(enemHRP.Position, enemyHRP.Position)}

ok swapped the code but:

function globalAnims.dashForw(model, enemModel)
	local charHRP = model:FindFirstChild("HumanoidRootPart")
	local enemHRP = enemModel:FindFirstChild("HumanoidRootPart")
	--Initially trying to make it face the enemy player wants to target immediately
	charHRP.CFrame = CFrame.lookAt(charHRP.Position, enemHRP.Position)
	
	local charTweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0)
	--Changed properties
	local properties = {CFrame = CFrame.new(enemHRP.Position, enemHRP.Position)}
	
	local tween = TS:Create(charHRP, charTweenInfo, properties)

	tween:Play()
end

image
he kinda just disappears and doesnt appear at the enemies location

To keep the rotation using CFrame.lookAt is much easier than you think.

local direction_cf = CFrame.lookAt(charHRP.Position, enemHRP.Position)
charHRP.CFrame = direction_cf

local properties = {CFrame = direction_cf.Rotation + enemHRP.Position}
-- // But since the players look direction was already changed to look at that direction
-- // Might as well just tween the position

local properties = {Position = enemHRP.Position}

There are other methods but this is using what you have. I’d like to also include that turning the Humanoid.AutoRotate option to false during these types of tweens makes it look a bit better; it makes the user using shift-lock or attempting to walk a certain direction while being tweened not turn.

2 Likes

Thanks a bunch, got a nice working solution and learned a bit of information about humanoids and tweening.

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