how to compare position between mc and other characters?

  • #1, by nerdThursday, 13. December 2018, 09:36 A year ago
    Since I'm trying to make a basic beat em up system, I'd need a way to compare mc and opponent position so that if mc is punching and y x cordinate beetween him and an opponent are between a determined range the punch triggers a pain animation to the opponent. How can I achieve this with a script?

    Forum Fan

    147 Posts


  • #2, by afrlmeThursday, 13. December 2018, 13:07 A year ago
    definition script
    function compareCharDist(atk, def)
     if Characters[atk].Position.x < Characters[def].Position.x and (Characters[def].Position.x - Characters[atk].Position.x) <= 100 then
     -- hit (attacker is on left)
    elseif if Characters[atk].Position.x > Characters[def].Position.x and (Characters[atk].Position.x - Characters[def].Position.x) <= 100 then
     -- hit (attacker is on right)
     end
    end
    1
    2
    3
    4
    5
    6
    7

    usage: (execute a script)
    compareCharDist("Tom", "Harry")
    1

    Imperator

    6816 Posts

  • #3, by NigecThursday, 13. December 2018, 13:18 A year ago
    I've been trying that setDelay example to see if I could detect when a character is in range and it seems to work well BUT I'm really not sure if its a good idea to have it running for an extended period of time also the range depends on how fast the delay runs.
    a delay of 250ms is enough if you check for a a character 50px away..

    Key Killer

    558 Posts

  • #4, by nerdThursday, 13. December 2018, 13:27 A year ago
    Thank you I am gonna try it

    Forum Fan

    147 Posts

  • #5, by afrlmeThursday, 13. December 2018, 13:48 A year ago
    I've been trying that setDelay example to see if I could detect when a character is in range and it seems to work well BUT I'm really not sure if its a good idea to have it running for an extended period of time also the range depends on how fast the delay runs.
    a delay of 250ms is enough if you check for a a character 50px away..

    Hence check directly via the relevant animation frame. Why bother with delays, when you can sync much easier by checking on the relevant animation frames?

    Imperator

    6816 Posts

  • #6, by nerdThursday, 13. December 2018, 14:26 A year ago
    Sorry for the naive question, but where do I write the definition script? Does it not go in "exectute script" right?

    Forum Fan

    147 Posts

  • #7, by afrlmeThursday, 13. December 2018, 15:22 A year ago
    In the script section of the editor. create a new script. by default it will be a definition type script.

    There are 2 script types...
    1. Definition: scripts of this type will be executed on game load - useful for declaring variables & functions.
    2. Execution: scripts of this type will be executed when called - useful for small/simple single use scripts.

    Imperator

    6816 Posts

  • #8, by afrlmeThursday, 13. December 2018, 15:25 A year ago
    In the script section of the editor. create a new script. by default it will be a definition type script.

    There are 2 script types...
    1. Definition: scripts of this type will be executed on game load - useful for declaring variables & functions.
    2. Execution: scripts of this type will be executed when called - useful for small/simple single use scripts.

    Quick note: the script I pasted was an example. It won't do anything unless you edit it & tell it what it's supposed to do. Such as play hurt animation of the opponent... maybe something along the lines of this...
    startAnimation(Characters[def].CurrentOutfit.CharacterAnimations["hurt"])
    1

    Imperator

    6816 Posts

  • #9, by nerdFriday, 14. December 2018, 09:15 A year ago
    function compareCharDist(MC,opponent)

    NearOpponentStandingRight =  getObject("Conditions[NearOpponentStandingRight]")
    NearOpponentStandingLeft =  getObject("Conditions[NearOpponentStandingLeft]")


     if Characters[MC].Position.x <; Characters[opponent].Position.x and (Characters[opponent].Position.x - Characters[MC].Position.x) <;= 100 then


    setCondition("nearEnough", true)
    NearOpponentStandingLeft:setValue(VConditionValue, true)
    NearOpponentStandingRight:setValue(VConditionValue, false)
    -- hit (attacker is on left)

    elseif


    Characters[MC].Position.x >; Characters[opponent].Position.x and (Characters[MC].Position.x - Characters[opponent].Position.x) <;= 100 then


    setCondition("nearEnough", true)
    NearOpponentStandingRight:setValue(VConditionValue, true)
    NearOpponentStandingLeft:setValue(VConditionValue, false)

     
    -- hit (attacker is on right)
     end
    end



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    compareCharDist("MC", "opponent")
    1

    I was wondering if it's ok, I added two variables that switch once the distance is in range.
    I added the execute script to a keyboard input to run it and check if the variable switch but it doesn't work. I also substitute the character names of the function, do they need quotation marks?

    edit: ignore "setCondition("nearEnough", true)" which I paste/copied by error

    Forum Fan

    147 Posts

  • #10, by nerdFriday, 14. December 2018, 10:01 A year ago
    function distanceScriptTry (mc, opponent)

    standingRight =  getObject("Conditions[standingRight]")
    standingLeft =  getObject("Conditions[standingLeft]")
    mc = getObject("Characters[MC]")
    opponent = getObject("Characters[opponent]")



    if mc.Position.x + opponent.Position.x >;=1

    and

    mc.Position.x + opponent.Position.x <;=300

    then

    standingRight:setValue(VConditionValue, true)

    else

    standingRight:setValue(VConditionValue, false)

    end

    if mc.Position.x - opponent.Position.x <;=-1

    and


    mc.Position.x - opponent.Position.x >;=-300

    then

    standingLeft:setValue(VConditionValue, true)

    else

    standingLeft:setValue(VConditionValue, false)

    end
    end

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    I also made this scritp which works for the left side, the standingLeft variable works and switches when distance is on range, but the standingRight variable doesn't work

    Forum Fan

    147 Posts

  • #11, by nerdFriday, 14. December 2018, 10:36 A year ago
    This works:

    function distanceScriptTry (mc, opponent)

    standingRight =  getObject("Conditions[standingRight]")
    standingLeft =  getObject("Conditions[standingLeft]")
    mc = getObject("Characters[MC]")
    opponent = getObject("Characters[opponent]")


    if mc.Position.x <; opponent.Position.x

    and

    mc.Position.x - opponent.Position.x >;=-300

    then

    standingLeft:setValue(VConditionValue, true)

    else

    standingLeft:setValue(VConditionValue, false)

    end

    if mc.Position.x >; opponent.Position.x

    and

    mc.Position.x - opponent.Position.x <;=300

    then

    standingRight:setValue(VConditionValue, true)

    else

    standingRight:setValue(VConditionValue, false)

    end
    end

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42

    Forum Fan

    147 Posts

Write post