• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Player turning towards target direction

poe6man3

Member
Joined
Aug 6, 2020
Messages
87
Solutions
1
Reaction score
12
Can somebody help me, i most likely would be able to write it my self but i just cant figure out the logic begind the positions my brain just steaming for 2 hours straight and nothing have worked so far



fae426979fdf9e092f75a04ab3b9fbfa.jpg


If anybody can change exori vis to work like that:
- On cast caster turning towards its target
if in area 1 turns north
if in area 2 turns east
if in area 3 turns south
if in area 4 turns west

thanks in advance​
 
Not home this weekend, but what you want to do is compare the position x and y values.

If pos1.x < pos2.x and pos1.y < pos2.y then
- - monster is north west, so turn monster south or east.
 
Not home this weekend, but what you want to do is compare the position x and y values.

If pos1.x < pos2.x and pos1.y < pos2.y then
- - monster is north west, so turn monster south or east.
Ye thats what i am doing all the time and i always end up with a squere check i just cant figure out the diagonal check from 1 pos to another
 
Ye thats what i am doing all the time and i always end up with a squere check i just cant figure out the diagonal check from 1 pos to another
Correct. You'll have 4 rentangles, which gives you your 4 directions
 
Correct. You'll have 4 rentangles, which gives you your 4 directions​
i did ur idea and its working partially good but there are spots that just should turn player in better direction
thats how i placed the checks
4c31e5324f4506fdc46b0bb1c5e61dfd.jpg


but when me and my target will be in this situation
1d8a8f9efb289eb572c9a54bd74d4f13.jpg


the caster is turning WEST instead of NORTH just like on the screen

the code i used
Lua:
local function turnTowardsEnemy(creature, targetPos)

    if not creature or creature:isRemoved() then return end
    if targetPos == nil then
        print("function turnTowardsEnemy need target position")
        return
    end
    local myPos = creature:getPosition()
   
    if targetPos.x < myPos.x and targetPos.y < myPos.y or targetPos.x < myPos.x and targetPos.y > myPos.y then
        creature:setDirection(WEST)
    elseif targetPos.x < myPos.x and targetPos.y < myPos.y or targetPos.x > myPos.x and targetPos.y < myPos.y then
        creature:setDirection(NORTH)
    elseif targetPos.x > myPos.x and targetPos.y < myPos.y or targetPos.x > myPos.x and targetPos.y > myPos.y then
        creature:setDirection(EAST)
    elseif targetPos.x > myPos.x and targetPos.y > myPos.y or targetPos.x < myPos.x and targetPos.y > myPos.y then
        creature:setDirection(SOUTH)
    end
end

Idk if i implemented that correctly how u said but u always can tell me i will gladly try to do it my self cause i would like to learn coding to the perfection if thats even possible for me :D
 
Last edited:
i did ur idea and its working partially good but there are spots that just should turn player in better direction
thats how i placed the checks
4c31e5324f4506fdc46b0bb1c5e61dfd.jpg


but when me and my target will be in this situation
1d8a8f9efb289eb572c9a54bd74d4f13.jpg


the caster is turning WEST instead of NORTH just like on the screen

the code i used
Lua:
local function turnTowardsEnemy(creature, targetPos)

    if not creature or creature:isRemoved() then return end
    if targetPos == nil then
        print("function turnTowardsEnemy need target position")
        return
    end
    local myPos = creature:getPosition()
  
    if targetPos.x < myPos.x and targetPos.y < myPos.y or targetPos.x < myPos.x and targetPos.y > myPos.y then
        creature:setDirection(WEST)
    elseif targetPos.x < myPos.x and targetPos.y < myPos.y or targetPos.x > myPos.x and targetPos.y < myPos.y then
        creature:setDirection(NORTH)
    elseif targetPos.x > myPos.x and targetPos.y < myPos.y or targetPos.x > myPos.x and targetPos.y > myPos.y then
        creature:setDirection(EAST)
    elseif targetPos.x > myPos.x and targetPos.y > myPos.y or targetPos.x < myPos.x and targetPos.y > myPos.y then
        creature:setDirection(SOUTH)
    end
end

Idk if i implemented that correctly how u said but u always can tell me i will gladly try to do it my self cause i would like to learn coding to the perfection if thats even possible for me :D
Untitled.png

Here's my shitty paint skills. xD

-x to +x
-y to +y

Think of the boxes like this when doing your checks.

Let's say we want to check for the pink box.
What do you need to check for?

We can clearly see that it's -x, and we can see that it is +y.
We can also see that 1 column is x, not -x or +x, just x.

Since x is negative and neutral for the pink box we use <= less then or equal to.
Think of current position as position (x = 0, y = 0, z = 0).
if target position is (x = -2, y = +2, z = 0) we can see that -2 is less then 0, and that 0 == 0, so we use less then or equal to. <=

Can do the same for y. +2 is more then 0, and the box never touches the centre line, so it's only above. so we use greater than >

So let's write the full equation.
Lua:
local cur_pos = blah
local tar_pos = blahblah
if tar_pos.x <= cur_pos.x and tar_pos.y > cur_pos.y then

Now when we check this area, since the south side of the black box touches the pink box, we just turn the character to face opposite. (north)

And then you do similar checks for the other 3 directions.

--

Of course this is just 1 way of doing these checks.
It's a simple and effective method.

Once you understand the basic concept, you can of course make the direction check more complex to account for the diagonals, et cetera, but it's quite a bit harder and will require more effort.

See what you can do on your own, and decide if it's worth the effort to do the more advanced way, or the simple way. :)
 
View attachment 58996

Here's my shitty paint skills. xD

-x to +x
-y to +y

Think of the boxes like this when doing your checks.

Let's say we want to check for the pink box.
What do you need to check for?

We can clearly see that it's -x, and we can see that it is +y.
We can also see that 1 column is x, not -x or +x, just x.

Since x is negative and neutral for the pink box we use <= less then or equal to.
Think of current position as position (x = 0, y = 0, z = 0).
if target position is (x = -2, y = +2, z = 0) we can see that -2 is less then 0, and that 0 == 0, so we use less then or equal to. <=

Can do the same for y. +2 is more then 0, and the box never touches the centre line, so it's only above. so we use greater than >

So let's write the full equation.
Lua:
local cur_pos = blah
local tar_pos = blahblah
if tar_pos.x <= cur_pos.x and tar_pos.y > cur_pos.y then

Now when we check this area, since the south side of the black box touches the pink box, we just turn the character to face opposite. (north)

And then you do similar checks for the other 3 directions.

--

Of course this is just 1 way of doing these checks.
It's a simple and effective method.

Once you understand the basic concept, you can of course make the direction check more complex to account for the diagonals, et cetera, but it's quite a bit harder and will require more effort.

See what you can do on your own, and decide if it's worth the effort to do the more advanced way, or the simple way. :)
Thank u very much for the help but in ur solution there are still same problem like on the second screen but i got great news i figured it out so i got question could anybody review te code that i made (im pretty proud of that one :D)


Lua:
local function turnTowardsEnemy(creature, targetPos)
  
    if not creature or creature:isRemoved() then return end
    if targetPos == nil then
        print("function turnTowardsEnemy need target position")
        return
    end

    local myPos = creature:getPosition()
    local range = myPos:getDistance(targetPos)

    local function checkNorth(i)
        local fromPos= Position(myPos.x - i, myPos.y - i, myPos.z)
        local toPos = Position(myPos.x + i, myPos.y - i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(NORTH)
        end
    end
  
    local function checkSouth(i)
        local fromPos= Position(myPos.x - i, myPos.y + i, myPos.z)
        local toPos = Position(myPos.x + i, myPos.y + i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(SOUTH)
        end
    end

    local function checkWest(i)
        local fromPos= Position(myPos.x - i, myPos.y + i , myPos.z)
        local toPos = Position(myPos.x - i, myPos.y - i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(WEST)
        end
    end
  
    local function checkEast(i)
        local fromPos= Position(myPos.x + i, myPos.y + i, myPos.z)
        local toPos = Position(myPos.x + i, myPos.y - i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(EAST)
        end
    end
  
    for i = 1, range do
        checkNorth(i)
        checkSouth(i)
        checkWest(i)
        checkEast(i)
    end

end
If anybody want the code u can take no problemo

The effects are just to check if stuff is working correctly hope anybody can give his review on my code :3

End resault:

https://i.gyazo.com/51e4d6c80f7bc97af43fcb007a0e1376.mp4
 
Thank u very much for the help but in ur solution there are still same problem like on the second screen but i got great news i figured it out so i got question could anybody review te code that i made (im pretty proud of that one :D)


Lua:
local function turnTowardsEnemy(creature, targetPos)
 
    if not creature or creature:isRemoved() then return end
    if targetPos == nil then
        print("function turnTowardsEnemy need target position")
        return
    end

    local myPos = creature:getPosition()
    local range = myPos:getDistance(targetPos)

    local function checkNorth(i)
        local fromPos= Position(myPos.x - i, myPos.y - i, myPos.z)
        local toPos = Position(myPos.x + i, myPos.y - i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(NORTH)
        end
    end
 
    local function checkSouth(i)
        local fromPos= Position(myPos.x - i, myPos.y + i, myPos.z)
        local toPos = Position(myPos.x + i, myPos.y + i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(SOUTH)
        end
    end

    local function checkWest(i)
        local fromPos= Position(myPos.x - i, myPos.y + i , myPos.z)
        local toPos = Position(myPos.x - i, myPos.y - i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(WEST)
        end
    end
 
    local function checkEast(i)
        local fromPos= Position(myPos.x + i, myPos.y + i, myPos.z)
        local toPos = Position(myPos.x + i, myPos.y - i, myPos.z)
        if targetPos:isInRange(fromPos, toPos) then
                fromPos:sendMagicEffect(50)
                toPos:sendMagicEffect(50)
                creature:setDirection(EAST)
        end
    end
 
    for i = 1, range do
        checkNorth(i)
        checkSouth(i)
        checkWest(i)
        checkEast(i)
    end

end
If anybody want the code u can take no problemo

The effects are just to check if stuff is working correctly hope anybody can give his review on my code :3

End resault:

https://i.gyazo.com/51e4d6c80f7bc97af43fcb007a0e1376.mp4
I'm home now, so I was able to quickly code something up for you. :)

I posted it over in Resources for anyone to use.

bandicam-2021-05-23-18-54-01-842.gif
 
Back
Top