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
However after the tween plays the character slowly rotates and ends up like this:
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)
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
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.