#How do i make the tower look at the enemy?

1 messages · Page 1 of 1 (latest)

steady crow
#

I was following 5uphi's tutorial on tower defense and i cannot seem to make the tower look at the enemy. In 5uphi's vi, he has stationary parts but i want to have a tower that looks at the enemy

    if self.Characters.Length == 0 then
        self.ActionTask = nil
    else
        self.AttackAnimation:Play()
        self.ActionTask = shared.Task:Delay(self.Cooldown, Action, self)
        self:Action()
    end
end```

here is the code for the attacking part
pseudo isle
#

And use lookAt to rotate the tower

steady crow
magic lance
#

with which u can get cframe of enemy

#

erm

#

mb

#

@steady crow

steady crow
#

Alr i did that and it works! But i have one issue, my tower rotates even if its not attacking

magic lance
#

that it can attack

#

You're russian?

steady crow
#

i used a different method compared to what you said.

here is my code

    local closestCharacter = nil
    local shortestDistance = self.Range
    local towerPosition = self.Model.PrimaryPart.Position

    for _, character in pairs(workspace.Characters:GetChildren()) do
        if character and character.PrimaryPart then
            local distance = (character.PrimaryPart.Position - towerPosition).Magnitude
            if distance <= self.Range and distance < shortestDistance then
                shortestDistance = distance
                closestCharacter = character
            end
        end
    end

    return closestCharacter
end

function class:TargetFunction()
    task.spawn(function()
        while self.Model and self.Model.Parent do
            local target = self:FindTarget()
            if target then
                self.Target = target
                print(target.Name)
                self:LookAtTarget(self.Target.PrimaryPart.Position)
            else
                self.Target = nil
            end

            task.wait()
        end
    end)
end

function class:LookAtTarget(targetPosition)
    local modelPrimary = self.Model.PrimaryPart
    if not modelPrimary then return end

    local towerPosition = modelPrimary.Position
    local targetVector = Vector3.new(targetPosition.X, towerPosition.Y, targetPosition.Z)
    local targetCFrame = CFrame.new(towerPosition, targetVector)

    modelPrimary.CFrame = targetCFrame
end```
steady crow
magic lance
#

but anywya

#

as I said, check if there is enemy to attack

#

or

#

wiat

#

uh

#

nvm

#

check if tower is on cooldown

#

basically

#

implement that rotation code

#

into attack code

#

use the rotate function in attack func

#

and it should be good

steady crow
#

ok will try

#
    local closestCharacter = nil
    local shortestDistance = self.Range
    local towerPosition = self.Model.PrimaryPart.Position

    for _, character in pairs(workspace.Characters:GetChildren()) do
        if character and character.PrimaryPart then
            local distance = (character.PrimaryPart.Position - towerPosition).Magnitude
            if distance <= self.Range and distance < shortestDistance then
                shortestDistance = distance
                closestCharacter = character
            end
        end
    end

    return closestCharacter
end

function class:TargetFunction()
    task.spawn(function()
        while self.Model and self.Model.Parent do
            local target = self:FindTarget()
            if target then
                self.Target = target
                print(target.Name)
                self:LookAtTarget(self.Target.PrimaryPart.Position)
            else
                self.Target = nil
            end

            task.wait()
        end
    end)
end

function class:LookAtTarget(targetPosition)
    local modelPrimary = self.Model.PrimaryPart
    if not modelPrimary then return end

    local towerPosition = modelPrimary.Position
    local targetVector = Vector3.new(targetPosition.X, towerPosition.Y, targetPosition.Z)
    local targetCFrame = CFrame.new(towerPosition, targetVector)

    modelPrimary.CFrame = targetCFrame
end

function Action(self)    
    if self.Characters.Length == 0 then
        self.ActionTask = nil
    else
        self.AttackAnimation:Play()
        self.ActionTask = shared.Task:Delay(self.Cooldown, Action, self)
        self:TargetFunction()
        self:Action()
    end
end

return class``` This is my code, it still didn't do anything. the tower still rotates even on cooldown
#

@magic lance

magic lance
#

I'm from phonw

#

Uh

#

You didn't understood me

steady crow
#

k

magic lance
#

Go to tower functions client

#

And put func calling there

#

In all attack types or whatever you want

#

That should work

steady crow
#

ok will try

#

ok i put it in tower functions and it doesn't change anything. still it rotates even if its not attacking

#

nvm i fixed the code

#

that while loop was the issue