Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make the part face the target's direction by only the X-Axis while tweening?

Asked by 5 years ago

I'm trying to make a part only face the target's direction only by the X-Axis while the part is moving towards the target with tweening. I tried mashing 2 functions together (the tween moving function and direction facing function) but obviously, it didn't work.

Code:

faceTarget = function(target, torso)
    if target ~= nil then
        local BG =  Instance.new("BodyGyro")
        BG.MaxTorque = Vector3.new(0, math.huge, 0)
        BG.P = 7500 -- // The point of this is to set the speed of how fast the AI turns
        BG.Parent = torso
    end
end

chaseAfter = function(zombie, torso)
    while true do
        wait()
        local target = findNearestTorso(torso.Position)     
        if target ~= nil then
            local info = TweenInfo.new(math.random(10))
            local goal = {}
            goal.CFrame = target.CFrame
            faceTarget(target, torso)
            local tween = tweenService:Create(torso, info, goal)
            tween:Play()
        end
    end
end

2 answers

Log in to vote
0
Answered by 5 years ago

Well using CFrame, to face in the direction of an object you would do...

CFrame.new(position, lookAtPosition)

So if you wanted to limit it to only the X-Axis, it would be...

-- Something similar to
CFrame.new(position, Vector3.new(target.Position.X, part.CFrame.lookVector.Y, part.CFrame.lookVector.Z)

And you could actually include that in the tween by doing...

...
local goal = {}
goal.CFrame = CFrame.new(target.Position, Vector3.new(target.Position.X, part.CFrame.lookVector.Y, part.CFrame.lookVector.Z)
...
0
it worked, but the only problem is that the zombies are facing backwards... fusionFSJAL 33 — 5y
Ad
Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago

What you could do is set the BodyGyro's MaxTorque to: Vector3.new(math.huge, 0, 0), so force is only applied on the x-axis.

Then set the bodyGyro's CFrame to: CFrame.new(torso.Position, target.Position), when you want the zombie AI to face the player

0
didnt work fusionFSJAL 33 — 5y

Answer this question