HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: Facing target  (Read 57 times)

Thazager

  • General Accounts
  • *
  • Posts: 1102
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Facing target
« on: Dec 19, 17, 02:09:41 PM »

Sometimes the game needs to check if the player is facing the target or not, and let the player know if they are not. This will add a check for the player facing their target. I found this code in HJRef and modified it to work for the E_ability scripts.


In E_AbilityClassMethods (server side) script, under this code segment:

Code: [Select]
  // check we have a target
  if( me.E_script:GetTargetType() == "Single" )
    if( target == None )
      return ResultUtils:ErrorWithMessage( "Invalid target." )
    .
  .

add this code segment:

Code: [Select]
  // check facing
  if not( CheckFacing(owner, target))
    return ResultUtils:ErrorWithMessage( "Not facing target." )
  .

add this function in the script also.


Code: [Select]
function CheckFacing(owner as NodeRef of Class E_CommonCharacter, target as NodeRef) as Boolean

  if owner is kindof E_playerCharacter
    MyPos as Vector3 = owner.GetPosition()
    MyRot as Vector3 = owner.getRotation()

    targPos as Vector3 = target.GetPosition()
    targRot as Vector3 = target.GetRotation()

    //Determine the angle of the activator (with auto-facing)
    angle as Float = ArcTangent((MyPos.x - targPos.x), (MyPos.z - targPos.z))

    //Adjust angle check to account for looping from -180 to 180 and vice versa
    width as Integer = 90

    Min1 as Float = angle - width
    Min2 as Float = Min1

    Max1 as Float = angle + width
    Max2 as Float = Max1

    if Min1 < -180
      Min2 = Min1 + 360
      Min1 = -180
      Max2 = 180
    .

    if Max1 > 180
      Max2 = Max1 - 360
      Max1 = 180
      Min2 = -180
    .

    if not((MyRot.y >= Min1 and MyRot.y <= Max1) or (MyRot.y >= Min2 and MyRot.y <= Max2))
      return false
    .
  .
  return true
.


Since the NPCs are usually made to face the target, we only check the player. It will print a small mesg to the chat window.

In the HJRef script, I found the code was using the Target Rotation instead of the User Rotation to check if they were at the right angle. (I don't think it should be checking the target angle.)
Logged
Heroes and Villains Lead programmer/scripter
Exile Online Lead programmer/scriptor

Thazager

  • General Accounts
  • *
  • Posts: 1102
  • Never stop learning
    • View Profile
    • Heroes and Villains MMORPG
Re: Facing target
« Reply #1 on: Dec 19, 17, 09:24:33 PM »

In testing the above function, I found it works based on player view, and not the character model for Rotation value. So, I created a client side version that will work based on the HBNode and not use the player view. The AddChatMessage() will be based on your function to add messages to chat window.

Code: [Select]
public function CheckFacing(target as Noderef) as Boolean

  var player = GetPlayerCharacterNode()
  if player <> None

    MyRot as Vector3 = GetNodeRotation(player)
    MyRot.y = MungeHeadingDegrees( MyRot.y - 90 )  // We subtract 90 when dealing with the HBNode's rotation

    MyPos as Vector3
    getNodePosition(player, MyPos)

    targPos as Vector3

    // type of target
    where target
      is kindof HBNode
        getNodePosition(target, targPos)
      .
      is kindof E_playerCharacter
        acct as NodeRef of Class E_playerAccount
        acct = target.GetMyAccount()
        getNodePosition(acct, targPos)
      .
    .

    //Determine the angle of the activator (with auto-facing)
    angle as Float = ArcTangent((MyPos.x - targPos.x), (MyPos.z - targPos.z))

    //Adjust angle check to account for looping from -180 to 180 and vice versa
    width as Integer = 90

    Min1 as Float = angle - width
    Min2 as Float = Min1

    Max1 as Float = angle + width
    Max2 as Float = Max1

    if Min1 < -180
      Min2 = Min1 + 360
      Min1 = -180
      Max2 = 180
    .

    if Max1 > 180
      Max2 = Max1 - 360
      Max1 = 180
      Min2 = -180
    .

    // check if facing target - if not send mesg
    if not((MyRot.y >= Min1 and MyRot.y <= Max1) or (MyRot.y >= Min2 and MyRot.y <= Max2))

      $CHAT.GetChatWindow().AddChatMessage("Not facing target.","Game")
      return false
    .
  .
  return true
.

Logged
Heroes and Villains Lead programmer/scripter
Exile Online Lead programmer/scriptor